Arrays.Aslist() Not Working as It Should

Arrays.asList() not working as it should?

How about this?

Integer[] ints = new Integer[] {1,2,3,4,5};
List<Integer> list = Arrays.asList(ints);

Arrays.asList(int[]) not working

When you pass an array of primitives (int[] in your case) to Arrays.asList, it creates a List<int[]> with a single element - the array itself. Therefore contains(3) returns false. contains(array) would return true.

If you'll use Integer[] instead of int[], it will work.

Integer[] array = {3, 2, 5, 4};

if (Arrays.asList(array).contains(3))
{
System.out.println("The array contains 3");
}

A further explanation :

The signature of asList is List<T> asList(T...). A primitive can't replace a generic type parameter. Therefore, when you pass to this method an int[], the entire int[] array replaces T and you get a List<int[]>. On the other hand, when you pass an Integer[] to that method, Integer replaces T and you get a List<Integer>.

Why Arrays.asList(...).contains(...) isn't working?

Your current approach is incorrect because when you do Arrays.asList(keyCodesArray) what you're getting is something like this:

List<char[]> 

In order to make Arrays.asList(keyCodesArray) to work properly, you'll need to change this:

final char[] keyCodesArray = {'w','a','s','d'};

to this:

final Character[] keyCodesArray = {'w','a','s','d'};

which essentially provides a List<Character> when you apply Arrays.asList(keyCodesArray), therefore:

if(Arrays.asList(keyCodesArray).contains(tmp)) { ... }

would work.

Arrays.asList(arr).indexOf is not working

The problem is the type of Array you're creating inside the asList(), Considering that the List types require non-primitive types, you need to declare int as Integer. If you change your code to:

Arrays.asList(new Integer[]{1,2,3,4,5}).indexOf(5);

It will work.

contains() method is not working for Arrays.asList in java

Your string variable color is not an array, so first of all you need to create array from that string variable with split(String dilemeter) method and create ArrayList from splitted string, like this:

List<String> arrList = Arrays.asList(color.split(", "));

After that you can check if arrList contains some element:

boolean check = arrList.contains("pink");

Java Arrays.asList not

You can't have a list of primitive type.

change j to

Integer [] j=new Integer[8];

Arrays.asList(word.toCharArray()) does not return a list?

What's going on here?

Here's the signature of Arrays.asList:

 public static <T> List<T> asList​(T... a)

First observation is that T must be an reference type. All Java type parameters are reference types.

Second observation is that a is a varargs parameter. That means that a can either be expressed as one or more T instances ... OR a T[].

To your example. It seems that you expect the following expression to produce a list of char or Character.

 Arrays.asList(word.toCharArray())

Firstly, List<char> is not a valid Java type, because char is not a reference type.

Secondly, List<Character> cannot be produced because that is not allowed by the signature. Lets try. If T is Character, then the substitution would be

   public static List<Character> asList(Character... a)

but Character actually means Character[], and word.toCharArray() produces a char[]. (And the Java language will not convert char[] to Character[].)

In fact, what actually happens is that the T matches char[], and the result of Arrays.asList will be a List<char[]>. And the object that you get will be a list of size 1, with a single char[] element which contains all the characters of word.

Arrays.asList(arrayname).contains(int) doesnt work

Integer[] AllCards = {1,2,3,4,5,6,7,8,9,10};

if(Arrays.asList(AllCards).contains(1))
System.out.println("yes");
else {
System.out.println("no");
}

to understand why check this : https://stackoverflow.com/a/1467940/4088809



Related Topics



Leave a reply



Submit