View RSS Feed

My Java Tips

Using Arrays in Java

Rate this Entry
by , 11-08-2011 at 06:24 PM (744 Views)
Arrays 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.



Java Code:
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;
}

Submit "Using Arrays in Java" to Facebook Submit "Using Arrays in Java" to Digg Submit "Using Arrays in Java" to del.icio.us Submit "Using Arrays in Java" to StumbleUpon Submit "Using Arrays in Java" to Google

Tags: array, size fixed Add / Edit Tags
Categories
Java SE

Comments