Page 1 of 9
Arrays:
An array
is a structure that holds multiple values of the
same type. The length of an array is established when
the array is created. After creation, an array is a
fixed-length structure.
An array element
is one of the values within an array
and is accessed by its position within the array.
Page 2 of 9
Creating and Using Arrays (another example)
public class ArrayDemo {
public static void main(String[] args) {
int[] anArray; // declare an array of integers
anArray = new int[10]; // create an array of integers // assign a value to each array element and print for (int i = 0; i < anArray.length; i++) {
anArray[i] = i;
System.out.print(anArray[i] + " "); }
System.out.println(); } //end main
} //end class
Declaring a Variable to Refer to an Array
int[] anArray;
float[] anArrayOfFloats;
Page 3 of 9
Creating an Array
new elementType[arraySize]
anArray = new int[10];
// create an array of
integers
Array Initializers
boolean[] answers = { true, false, true,
true, false };
Accessing Array Elements
for (int i = 0; i < anArray.length; i++) {
anArray[i]
= i;
System.out.print(
anArray[i]
+ " ");
}
Getting the Size of an Array
arrayName.length
Page 4 of 9
Arrays of Objects
public class ArrayOfStringsDemo {
public static void main(String[] args) {
String[] anArray = {
"String One",
"String Two",
"String Three"};
for (int i = 0; i<anArray.length; i++) {
System.out.println(anArray[i].toLowerCase());
}
}
}
Arrays of Arrays (Multi-dimensional arrays)
Check the
MultiDimArrayDemo
example in the
tutorial.
public class ArrayOfArraysDemo {
public static void main(String[] args) {
String[][] countries = {
{ "North America", “USA", "Mexico", "Canada”},
{ "South America","Brazil", "Argentina", Uruguey"}, { "Europe", "England", "Germany", "Netharland", "Spain", "Itally"}
};
for (int i = 0; i < countries.length; i++) { System.out.print(countries [i][0] + ": ");
for (int j = 1; j < countries [i].length; j++) { System.out.print(countries [i][j] + " "); }
System.out.println(); }
Page 5 of 9
Another example of Arrays of Arrays
public class ArrayOfArraysDemo2 {
public static void main(String[] args) {
int[][] aMatrix = new int[4][];
//populate matrix
for (int i = 0; i < aMatrix.length; i++) {
aMatrix[i] = new int[i+1]; //create sub-array for (int j = 0; j < aMatrix[i].length; j++) {
aMatrix[i][j] = i + j; }
}
//print matrix
for (int i = 0; i < aMatrix.length; i++) {
for (int j = 0; j < aMatrix[i].length; j++) { System.out.print(aMatrix[i][j] + " "); }
System.out.println(); }
Page 6 of 9
Stack example
/**
* @author Nagesh Chauhan (slightly edited) */
public class StackDemo {
static final int capacity = 3; int arr[] = new int[capacity];
int top = -1;
public void push(int pushedElement) { if (top < capacity - 1) {
top++; //top = top+1
arr[top] = pushedElement;
System.out.println("Element " + pushedElement + " is pushed to Stack !");
printElements(); } else {
System.out.println("Stack Overflow !"); }
}
public void pop() { if (top >= 0) {
top--; //top = top-1
System.out.println("Pop operation done !"); } else {
System.out.println("Stack Underflow !"); }
}
public void printElements() { if (top >= 0) {
System.out.println("Elements in stack :"); for (int i = 0; i <= top; i++) {
System.out.println(arr[i]); }
Page 7 of 9
public static void main(String[] args) { StackDemo stackDemo = new StackDemo();
stackDemo.pop(); stackDemo.push(23); stackDemo.push(2); stackDemo.push(73); stackDemo.push(21); stackDemo.pop(); stackDemo.pop(); stackDemo.pop(); stackDemo.pop(); }
}
OUTPUT:
Stack Underflow !
Element 23 is pushed to Stack ! Elements in stack :
23
Element 2 is pushed to Stack ! Elements in stack :
23 2
Element 73 is pushed to Stack ! Elements in stack :
23 2 73
Page 8 of 9
Copying Arrays
public static void
arraycopy(Object source, int srcIndex, Object dest, int destIndex, int length)
Example:
class ArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo)); }
Page 9 of 9
Array manipulations
Java SE provides several methods for performing array
manipulations (common tasks, such as copying, sorting and
searching arrays) in the
java.util.Arrays
class.
class ArrayCopyOfDemo {
public static void main(String[] args) {
char[] copyFrom = {'d', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd'};
char[] copyTo = java.util.Arrays.copyOfRange(copyFrom, 2, 9);
System.out.println(new String(copyTo)); }
}
Some other useful operations provided by methods in
the
java.util.Arrays
class, are:
Searching an array for a specific value to get the index at
which it is placed (the binarySearch method).
Comparing two arrays to determine if they are equal or
not (the
equals
method).
Filling an array to place a specific value at each index
(the
fill
method).
Sorting an array into ascending order. This can be done
either sequentially, using the sort method, or
concurrently, using the
parallelSort
method