Java Arrays
To declare an array of integers, you start with:
int[] myArray;
To create an array of ten integers, you can try:
myArray = new int[10];
To set the values โโin this array, try:
myArray[0] = 1;
Or when instantiating:
int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
To get values โโfrom an array, try:
System.out.println(myArray[0]);
To print all the values โโin an array, try:
// go from 0 to one less than the array length, based on 0 indexing for(int i = 0; i < myArray2.length; i++) { System.out.println(myArray2[i]); }
For more information, a tutorial from Sun / Oracle will be very helpful. You can also check the Java language specification in arrays .
Using the array utility class
java.util.Arrays contains a bunch of static methods . Static methods are class-specific and do not require an instance of the class to be called. Instead, they are called with the class name as a prefix.
So you can do the following:
or
justkt
source share