0

How can improve this algorithm in order to use a dynamic validation source?

public static void main(String[] args) 
{
    int[] exclude = {1, 4, 7};

    for(int i= 0 ; i < 10; i++)
    { 
        if(i != 1 || i != 4 || i != 7);
        {
            continue;
        }
    }
        System.out.println(i);
    }
}

Meaning, I want to instead of the if inside the for loop, I want to receive a list and validate the content of must-exclude elements, maybe something like:

        if(Arrays.asList(exclude).contains(i))

Just for the sake of knowing how to do it. I feel like it might potentially be useful in the future. Basically I want to skip certain iterations in a loop using an array.

How

Garis M Suero
  • 7,974
  • 7
  • 45
  • 68
theaero
  • 53
  • 1
  • 2
  • 5
  • 1
    What's the question? Does that not work? – Carl Norum Mar 06 '13 at 01:34
  • Fix the indentation on your first sample. It makes no sense as written - correct indentation would make this apparent. Anyway, there are often many ways to do something. If you just need a "contains", however, consider a Set. Not that it makes a difference here, as it's a trivial n and small S. The `asList` each loop is also particular "wasteful" but, again, it doesn't matter here. –  Mar 06 '13 at 01:35
  • No it does not work as I have it written. It prints all numbers 0-9. – theaero Mar 06 '13 at 01:50
  • @pst, I must have accidentally deleted the newline, making the formatting kind of weird in the original example. Sorry! – theaero Mar 06 '13 at 01:51

2 Answers2

0

This call:

Arrays.asList(exclude)

Actually it creates a List, which is not what you want. If you declared you exclude array as an array of Integer, it would work:

public static void main(String[] args) 
{
    Integer[] exclude = {1, 4, 7};

    for(int i= 0 ; i < 10; i++)
    {
       if(!Arrays.asList(exclude).contains(i))
       {
           System.out.println(i);
       }
    }
}

The thing is, you were providing an int[] to this Arrays.asList. The compiler has to decide if it should interpret the generic parameter T as int or as int[]. But it can not chose int because you can not have a integral type as a generic type. It could chose Integer, but it would mean a conversion, and int[] is therefore not direct. So T is int[] and it creates a List<int[]> with only the array you provided as element. And no integer is equals to that array as a whole of course. That is why all integers are printed.

Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58
  • Thanks, this works (after removing the i++ in the if statement) However, I'm curious. What is the difference between Interger[] and Int[]? – theaero Mar 06 '13 at 01:58
  • Would there be a more efficient way to do this as well? Thanks, sorry for all the questions. I'm learning! – theaero Mar 06 '13 at 02:00
  • Well, you could have created a list before the loop itself like this: `List exclude = Arrays.asList(1,4,7);`. I don't see a better method. – Cyrille Ka Mar 06 '13 at 02:02
  • I just read your edit. Thank you, it was helpful. However, the i++ in the if statement increments i an additional time. I think it should be removed to function correctly. – theaero Mar 06 '13 at 02:02
  • You know that unless the compiler is _very_ clever (as it certainly is _not_ in Android and probably in the Sun JITC as well), this code causes the array to be converted to an ArrayList 10 times, then it searches in linear time. If there are many iterations, you should build the ArrayList one time. If there are many exclusions, you should build a HashSet instead. The garbage created by gratuitous conversions like this tends to be okay when a program is new and customers are few and later cause nasty problems when it's old and the customer base has grown. – Gene Mar 06 '13 at 02:15
-1

It seems like you've already got it?

public static void main(String[] args) 
{
    int[] exclude = {1, 4, 7};

    for(int i= 0 ; i < 10; i++)
    {
       if(Arrays.asList(exclude).contains(i))
       {
           i++;
       }
       else 
       {
           System.out.println(i);
       }
    }
}