Arrays with Java Technology
- What is Array?
- Declaring Array in Java
- Working with 1D , 2D and Multidimensional Array
- Non-Rectangular Array
- Examples of Array
Arrays in Java |
- Arrays, classes, and interfaces are reference types. The value of a reference type variable, in contrast to that of a primitive type, is a reference to (an address of) the value or set of values represented by the variable.
- A reference is called a pointer, or a memory address in other languages. The Java programming language does not support the explicit use of addresses like other languages do. You use the variable’s name instead. An array is an important data structure in any programming language.
- An array is a fixed-length structure that stores multiple values of the same type. You can group values of the same type within arrays. Arrays are supported directly by the Java programming language; there is no array class. Arrays are implicit extensions of the Object class, so you can assign an array to a variable whose type is declared as Object.
- The Java platform groups its classes into functional packages. Instead of writing your own classes, you can use one provided by the platform. Most of the classes discussed till now are members of the java.lang package.
- All the classes in the java.lang package are available to your programs automatically.
Declaring Arrays |
Group data objects of the same type.
Declare arrays of primitive or class types.
char s[]; or char [] s;
Point p[]; or Point [] p;
- Create space for reference.
- An array is an object, not memory reserved for primitive types
- Like declarations for variables of other types, an array declaration has two components: the array’s type and the array’s name. An array’s type is written type[], where type is the data type of the elements contained within the array, and [] indicates that this is an array. Remember that all of the elements within an array are of the same type. Here are declarations for arrays that hold other types of data:
float[] anArrayOfFloats;
boolean[] anArrayOfBooleans;
Object[] anArrayOfObjects;
- As with declarations for variables of other types, the declaration for an array variable does not allocate any memory to contain the array elements.
- Arrays can hold reference types as well as primitive types. You create such an array in much the same way you create an array with primitive types
String[] anArrayOfStrings;
Creating Arrays
Use the new keyword to create an array object.
s = new char[20];
p = new Point[100];
p[0] = new Point();
p[1] = new Point();
.
.
p[99]=new Point();
Initializing Arrays
- Initialize an array element
- Create an array with initial values
String names[] = new String[3];
names [0] = "Jack";
names [1] = "Jill";
names [2] = "Tom";
XClass array[] = { new XClass(), new XClass(), new XClass() };
Working with 2D and Multidimensional Array |
Multi-Dimensional Arrays
Array of arrays :
int twoDim [] [] = new int [4] [];
twoDim [0] = new int [5];
twoDim [1] = new int [5];
int twoDim [] [] = new int [] [5]; // illegeal
Non-Rectangular arrays of arrays
twoDim [0] = new int[17];
twoDim [1] = new int[2];
twoDim [2] = new int[11];
twoDim [3] = new int[7];
Array of four arrays of five integers each :
int twoDim[] [] = new int[4][5];
Array Bounds
– All array subscripts begin with 0 :
int list [] = new int[10];
for (int i = 0; i< list.length; i++)
{
System.out.println(list[i]);
}
Note:- Special care should be taken while accessing an array element, using the length variable of array objects. In the example shown above list.length gives 10, and if we try to access list[10], we will get an exception.
Array Resizing
- Can not resize an array
- Can use the same reference variable to refer to an entirely new array.
int elements [] = new int[5];
elements = new int[10];
Note :- Arrays once defined can not be resized or redefined to store a different type of data. If we use the same reference to refer to a new array object, we will loose the previous array object which was referred by the same reference.
Copying Arrays
The System.arraycopy() method :
// original array
int elements [] = {1, 2, 3, 4, 5, 6};
// new larger array
int hold [] = {10,9,8,7,6,5,4,3,2,1};
// copy all members of, the elements array, to
//hold array, starting with the 0th index
System.arraycopy(elements,0,hold,0,elements.length);
- Use System's arraycopy method to efficiently copy data from one array into another.
- The arraycopy method requires five arguments:
public static void arraycopy(Object source, int srcIndex,Object dest,int destIndex,int length)
- The two Object arguments indicate the array to copy from and the array to copy to.
- The three integer arguments indicate the starting location in each of the source and the destination array, and the number of elements to copy.
- The arraycopy method call in this example program begins the copy at element number 0 in the source array. Recall that array indices start at 0, so that the copy begins at the array element â1'.
- The arraycopy method call puts the copied elements into the destination array beginning at the first element (element 0) in the destination array hold.
Note:- that the destination array must be allocated before you call arraycopy and must be large enough to contain the data being copied.
class ArrayCopyDemo
{
public static void main(String[] args)
{
char[] copyFrom = { 'm', 'y', 'd', 'r', 'e', 'a', 'm','s', 'c', 'o', 'm', 'e'};
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 6);
System.out.println(new String(copyTo));
}
}
/*
The output from this program is:- dreams
*/
Working with 1D Array
Non-Rectangular Array |
Non-Rectangular arrays of arrays
int twoDim [] [] = new int [4] [];
twoDim [0] = new int[17];
twoDim [1] = new int[2];
twoDim [2] = new int[11];
twoDim [3] = new int[7];
Examples of Array |
Recent Comments