Using Arrays in Java
5 April 2007Arrays are simply a sequence of memory locations for storing data. The index of an array starts from zero. We have to mention the size of an array while declaring it. Once declared, we cannot change the size at any stage.
While working with arrays, you will encounter ArrayIndexOutOfBoundsException. This exception is raised if you try to access an element from the array with negative index, or with index greater than the length of index.
Sorting an array is easy in Java because the Arrays class (from util package) does all of the work for you so you do not need to implement any algorithm for sorting.
Review following code for better understanding.
int[] myarray = new int[10]; //declare int array of size 10 //filling array for (int i=0; i<10;i++) { myarray[i]=i+1; } //printing elements of array for (int i=0; i<10;i++) { System.out.println(myarray[i]); } //creating an array of two strings String[] strArray = new String[2]; //array's first element has index 0 strArray[0] = "I am first"; //array's first element has index 1 strArray[1] = "I am second"; //size of array is 2 System.out.println(strArray.length); //with throw ArrayIndexOutOfBoundsException System.out.println(strArray[2]); //Our array is of size 2 but if we try to access strArray[2], //we will get ArrayIndexOutOfBoundsException. //following code show how to sort arrays import java.util.*; public class ArrayTestClass { public static void main(String[] str) { System.out.println("Hello World"); String names[] = { "Dave", "Nauman", "Moris", "Beven", "Duffy", "Sooma", "Rob", }; System.out.println("The original order of Array:"); for (int i = 0; i < names.length; i++) System.out.println(i + ": " + names[i]); // sorting array Arrays.sort(names); System.out.println("The new order of array:"); for (int i = 0; i < names.length; i++) System.out.println(i + ": " + names[i]); } } //You can also declare and use multidimentional arrays in Java. int[][] workArray = new int[5][5]; //initializing array with 1 for(int i=0; i<5; i++) { for(int j=0; j<5; j++) workArray[i][j]=1; } int[] myarray = new int[10]; //declare int array of size 10 //filling array for (int i=0; i<10;i++) { myarray[i]=i+1; } //printing elements of array for (int i=0; i<10;i++) { System.out.println(myarray[i]); } //creating an array of two strings String[] strArray = new String[2]; //array's first element has index 0 strArray[0] = "I am first"; //array's first element has index 1 strArray[1] = "I am second"; //size of array is 2 System.out.println(strArray.length); //with throw ArrayIndexOutOfBoundsException System.out.println(strArray[2]); //Our array is of size 2 but if we try to access strArray[2], //we will get ArrayIndexOutOfBoundsException. //following code show how to sort arrays import java.util.*; public class ArrayTestClass { public static void main(String[] str) { System.out.println("Hello World"); String names[] = { "Dave", "Nauman", "Moris", "Beven", "Duffy", "Sooma", "Rob", }; System.out.println("The original order of Array:"); for (int i = 0; i < names.length; i++) System.out.println(i + ": " + names[i]); // sorting array Arrays.sort(names); System.out.println("The new order of array:"); for (int i = 0; i < names.length; i++) System.out.println(i + ": " + names[i]); } } //You can also declare and use multidimentional arrays in Java. int[][] workArray = new int[5][5]; //initializing array with 1 for(int i=0; i<5; i++) { for(int j=0; j<5; j++) workArray[i][j]=1; }
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Using Arrays in Java
And not to forget the utility class java.util.Arrays, what you already used for sorting. More useful methods are:
- binarySearch()
- copyOf() and copyOfRange()
- equals() and deepEquals()
- fill()
- hashCode() and deepHashCode()
- toString()
Arrays.asList(arrays) for getting a java.util.List is very useful too. So you can search for an element, e.g. Arrays.asList(args).contains(”-h”).