Sunteți pe pagina 1din 4

JAVA - ARRAYS

http://www.tutorialspoint.com/java/java_arrays.htm

Copyright tutorials point.com

Java provides a dat a st ruct ure, t he array, which st ores a fixed-size sequent ial collect ion of
element s of t he same t ype. An array is used t o st ore a collect ion of dat a, but it is oft en more useful
t o t hink of an array as a collect ion of variables of t he same t ype.
Inst ead of declaring individual variables, such as number0, number1, ..., and number99, you declare
one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] t o
represent individual variables.
This t ut orial int roduces how t o declare array variables, creat e arrays, and process arrays using
indexed variables.

Declaring Array Variables:


To use an array in a program, you must declare a variable t o reference t he array, and you must
specify t he t ype of array t he variable can reference. Here is t he synt ax for declaring an array
variable:
dataType[] arrayRefVar;

// preferred way.

or
dataType arrayRefVar[];

//

works but not preferred way.

No te: The st yle dataT ype[] arrayRefVar is preferred. The st yle dataT ype arrayRefVar[]
comes from t he C/C++ language and was adopt ed in Java t o accommodat e C/C++ programmers.

Example:
The following code snippet s are examples of t his synt ax:
double[] myList;

// preferred way.

or
double myList[];

//

works but not preferred way.

Creat ing Arrays:


You can creat e an array by using t he new operat or wit h t he following synt ax:
arrayRefVar = new dataType[arraySize];

The above st at ement does t wo t hings:


It creat es an array using new dat aType[arraySize];
It assigns t he reference of t he newly creat ed array t o t he variable arrayRefVar.
Declaring an array variable, creat ing an array, and assigning t he reference of t he array t o t he variable
can be combined in one st at ement , as shown below:
dataType[] arrayRefVar = new dataType[arraySize];

Alt ernat ively you can creat e arrays as follows:


dataType[] arrayRefVar = {value0, value1, ..., valuek};

The array element s are accessed t hrough t he index. Array indices are 0-based; t hat is, t hey st art
from 0 t o arrayRefVar.length-1.

Example:
Following st at ement declares an array variable, myList , creat es an array of 10 element s of double
t ype and assigns it s reference t o myList :
double[] myList = new double[10];

Following pict ure represent s array myList . Here, myList holds t en double values and t he indices are
from 0 t o 9.

Processing Arrays:
When processing array element s, we oft en use eit her for loop or foreach loop because all of t he
element s in an array are of t he same t ype and t he size of t he array is known.

Example:
Here is a complet e example of showing how t o creat e, init ialize and process arrays:
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " ");
}
// Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
System.out.println("Total is " + total);
// Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}
System.out.println("Max is " + max);
}
}

This would produce t he following result :


1.9
2.9
3.4
3.5
Total is 11.7

Max is 3.5

The foreach Loops:


JDK 1.5 int roduced a new for loop known as foreach loop or enhanced for loop, which enables you t o
t raverse t he complet e array sequent ially wit hout using an index variable.

Example:
The following code displays all t he element s in t he array myList :
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (double element: myList) {
System.out.println(element);
}
}
}

This would produce t he following result :


1.9
2.9
3.4
3.5

Passing Arrays t o Met hods:


Just as you can pass primit ive t ype values t o met hods, you can also pass arrays t o met hods. For
example, t he following met hod displays t he element s in an int array:
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}

You can invoke it by passing an array. For example, t he following st at ement invokes t he print Array
met hod t o display 3, 1, 2, 6, 4, and 2:
printArray(new int[]{3, 1, 2, 6, 4, 2});

Ret urning an Array from a Met hod:


A met hod may also ret urn an array. For example, t he met hod shown below ret urns an array t hat is
t he reversal of anot her array:
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
result[j] = list[i];
}
return result;
}

The Arrays Class:


The java.ut il.Arrays class cont ains various st at ic met hods for sort ing and searching arrays, comparing
arrays, and filling array element s. These met hods are overloaded for all primit ive t ypes.

SN
1

Metho ds with Descriptio n


public static int binarySearch(Object[] a, Object key)
Searches t he specified array of Object ( Byt e, Int , double, et c.) for t he specified value using
t he binary search algorit hm. The array must be sort ed prior t o making t his call. This ret urns
index of t he search key, if it is cont ained in t he list ; ot herwise, (-(insert ion point + 1).

public static bo o lean equals(lo ng[] a, lo ng[] a2)


Ret urns t rue if t he t wo specified arrays of longs are equal t o one anot her. Two arrays are
considered equal if bot h arrays cont ain t he same number of element s, and all corresponding
pairs of element s in t he t wo arrays are equal. This ret urns t rue if t he t wo arrays are equal.
Same met hod could be used by all ot her primit ive dat a t ypes (Byt e, short , Int , et c.)

public static vo id fill(int[] a, int val)


Assigns t he specified int value t o each element of t he specified array of int s. Same met hod
could be used by all ot her primit ive dat a t ypes (Byt e, short , Int et c.)

public static vo id so rt(Object[] a)


Sort s t he specified array of object s int o ascending order, according t o t he nat ural ordering
of it s element s. Same met hod could be used by all ot her primit ive dat a t ypes ( Byt e, short ,
Int , et c.)

S-ar putea să vă placă și