Converting array to list in Java

In your example, it is because you can’t have a List of a primitive type. In other words, List<int> is not possible. You can, however, have a List<Integer> using the Integer class that wraps the int primitive. Convert your array to a List with the Arrays.asList utility method. See this code run live at IdeOne.com.

What’s the simplest way to print a Java array?

Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you’re asking. Examples: Simple Array: String[] array = new String[] {“John”, “Mary”, “Bob”}; System.out.println(Arrays.toString(array)); Output: [John, Mary, Bob] Nested Array: … Read more

Initializing an array of objects

I’m currently working on a card game, and I’m having trouble with some initialization code: The trouble is that my compiler’s telling me that cards++ is not an l-value. I’ve read up on the whole pointer-array equivalence thing, and I thought I understood it, but alas, I can’t get it to work. My understanding is … Read more

How can I remove a specific item from an array?

Find the index of the array element you want to remove using indexOf, and then remove that index with splice. The splice() method changes the contents of an array by removing existing elements and/or adding new elements. Expand snippet The second parameter of splice is the number of elements to remove. Note that splice modifies … Read more

What’s the simplest way to print a Java array?

Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array. The output is even decorated in the exact way you’re asking. Examples: Simple Array:String[] array = new String[] {“John”, “Mary”, “Bob”}; System.out.println(Arrays.toString(array)); Output:[John, Mary, Bob] Nested Array:String[][] deepArray = new String[][] {{“John”, “Mary”}, {“Alice”, “Bob”}}; System.out.println(Arrays.toString(deepArray)); //output: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)