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"}}; // Gives undesired output: System.out.println(Arrays.toString(deepArray)); // Gives the desired output: System.out.println(Arrays.deepToString(deepArray));Output:
[[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922] [[John, Mary], [Alice, Bob]]doubleArray:double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 }; System.out.println(Arrays.toString(doubleArray));Output:
[7.0, 9.0, 5.0, 1.0, 3.0 ]intArray:int[] intArray = { 7, 9, 5, 1, 3 }; System.out.println(Arrays.toString(intArray));Output:
[7, 9, 5, 1, 3 ]
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"}}; // Gives undesired output: System.out.println(Arrays.toString(deepArray)); // Gives the desired output: System.out.println(Arrays.deepToString(deepArray));Output:
[[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922] [[John, Mary], [Alice, Bob]]doubleArray:double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 }; System.out.println(Arrays.toString(doubleArray));Output:
[7.0, 9.0, 5.0, 1.0, 3.0 ]intArray:int[] intArray = { 7, 9, 5, 1, 3 }; System.out.println(Arrays.toString(intArray));Output:
[7, 9, 5, 1, 3 ]
Always check the standard libraries first.
import java.util.Arrays;
Then try:
System.out.println(Arrays.toString(array));
or if your array contains other arrays as elements:
System.out.println(Arrays.deepToString(array));
[Java] Why can't you just print an array with System.out.println(array);
[JAVA]Console only prints memory address instead of list.
You'll want to either iterate through each element of the array and print it separately, or use Arrays.toString(array) to print the whole thing.
The default toString() method for arrays in java is made to print out a memory address.
See this stackoverflow question: https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array
More on reddit.com[java] How to access the last element in a two dimensional array of unknown size?
[Java] Needing to output an array via JOptionPane and search the array
If I understand correctly, you want an easier way to concatenate the contents of the array?
Two options that come to mind.
Use a loop, although this will leave an extra space on the end:
String contents = ""; for (int i = 0; i < ID.length; i++) { contents += ID[i]; contents += " "; }
Even better, use String.join:
String contents = String.join(" ", ID);
Now for your second issue, it's because you are using == when comparing strings on line 39. Instead, you want to use retID.equals(studentID[x]). See here for an explanation as to why. For the most part, when comparing Objects, you will want to use equals(). == just checks that the memory addresses of the two arguments match.
More on reddit.com