I am new to Java.
Coming from Python, I find it way more tedious to deal with arrays in Java than Python.
Specifically, to access arrays' positions.
This is the Python code I want to translate to Java:
# Access first 3 elements
arr = [1, 2, 3, 4, 5]
arr[:3]
output: [1, 2, 3]
But in Java, there is no "colon", and the shortest way I have found to get the first three elements is throuh a loop.
Is there a package or a non-loop way to do this?
System.out.println(java.util.Arrays.toString(subarray));should do the trick.System.out.println(array)it will calltoString()on the array which returns the result.Arrays.toString(array)basically iterates over the array, callstoString()on each element and returns a list of those strings which are separated by a comma.