Integer integrity check in Java array does not return expected values

I have looked in many places online and everyone seems to give me the same solution. Therefore, it is obvious that I made some stupid mistake that I do not see. Can someone please call me in the right direction. Thanks for the mill.

Here is my code:

import java.util.Arrays;

public class Solution { 
    public static void main(String[] args) {
        int[] outcomes = {1, 2, 3, 4, 5, 6};        
        int count = 0;      
        for(int y = 1; y<=6; y++){      
            if(Arrays.asList(outcomes).contains(y)){                
                count++;                
                System.out.println("outcomes contains "+ y);                
            }               
        }           
        System.out.println(count);  
    }

The final result should be 6, but equal to 0.

+4
source share
2 answers
Arrays.asList(int[])

returns a list of one item. One element is the one int[]you went through.

If you change the announcement

int[] outcomes

to

Integer[] outcomes

You will get the expected result.

+6
source

Two things should be fixed in your code:

  • int [] Integer [] ( )
  • .asList . 6 .

:

public static void main(String[] args) {

    Integer[] outcomes = {1, 2, 3, 4, 5, 6};
    List outcomesList = Arrays.asList(outcomes);
    int count = 0;

    for(int y = 1; y<=6; y++){
        if(outcomesList.contains(y)){
            count++;
            System.out.println("outcomes contains "+ y);
        }
    }
    System.out.println(count);
}
0

All Articles