Professional Documents
Culture Documents
Array
Array
Arrays
Arrays are used to store multiple values in a
single variable, instead of declaring separate
variables for each value.
Consider a situation, where we need to store 5
integer numbers.
Arrays Continue..
Then we need 5 variables of int data type and program
will be something as follows:
int number1;
int number2;
int number3;
int number4;
int number5;
Arrays Continue..
It was simple, because we had to store just 5
integer numbers.
Now let's
assume we have to store 5000 integer numbers,
so what is next? Are we going to use 5000
variables?
Arrays Continue..
To handle such situation, almost all the programming
languages provide a concept called the array.
An array
Is a data structure, which can store a fixed-size
collection of elements of the same data type.
Arrays Continue..
Note:
you can’t store different types in a single array.
Create Arrays
To create an array in Java, you use three steps:
1. Declare a variable to hold the array.
2. Create a new array object and assign it to the
array variable.
3. Store things in that array.
Declaring Array Variables
Array variables indicate the type of object the
array will hold (just as they do for any variable)
and the name of the array, followed by empty
brackets ([])
int temps[]; OR
int[] temps;
Creating Array Objects
The second step is to create an array object and
assign it to that variable. There are two ways to
do this
The first way is to use the new operator to
create a new instance of an array:
String[] names = new String[10];
Creating Array Objects
When you create an array object using new, all
its elements are initialized for you (0 for numeric
arrays, false for boolean, ‘\0’ for character
arrays, and null for everything else)
Creating Array Objects
You can also create and initialize an array at the same
time. Instead of using new to create the new array object,
enclose the elements of the array inside braces,
separated by commas:
String[] chiles = { “jalapeno”, “anaheim”,“serrano,”
“habanero,” “thai” }
Creating Array Objects
An array the size of the number of elements
you’ve included will be automatically created for
you.
Accessing Array Elements
To get at a value stored within an array, use the
array subscript expression:
myArray[subscript];
Note:
The myArray part of this expression is a variable
holding an array object.
Accessing Array Elements
Subscript is the slot within the array to access.
Array subscripts start with 0.
You can test for the length of the array in your
programs using the length instance variable
Accessing Array Elements
It’s available for all array objects, regardless of
type
Changing Array Elements
To change the value of a specific element, refer to
the index number:
Example
cars[0] = "Opel";
Example
String[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
// Now outputs Opel instead of Volvo
Array Length
To find out how many elements an array has, use the
length property:
Example
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
// Outputs 4
Loop Through an Array
You can loop through the array elements with
the for loop, and use the length property to
specify how many times the loop should run
Example
String[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
Enhanced for loop in Java
This is mainly used to traverse collection of elements including
arrays.
Syntax:
The syntax of enhanced for loop is:
for(declaration : expression)
{
//Statements
}
Declaration:
The newly declared block variable, which is of a
type compatible with the elements of the array
you are accessing.
The variable will be available within the for
block and its value would be the same as the
current array element.
Expression:
This evaluates to the array you need to loop
through. The expression can be an array
variable or method call that returns an array.
Example
public class Test {
public static void main(String args[]){
int [] numbers = {10, 20, 30, 40, 50};{
}
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
}
Multidimensional Arrays
Java does not support multidimensional arrays.
However, you can declare and create an array
of arrays, and access them as you would C-
style multidimensional arrays:
Multidimensional Arrays
int coords[][] = new int[12][12];
coords[0][0] = 1;
coords[0][1] = 2;
class MultiDimArrayDemo {
public static void main(String[] args) {
String[][] names = {
{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}
};
// Mr. Smith
System.out.println(names[0][0] + names[1][0]);
// Ms. Jones
System.out.println(names[0][2] + names[1][1]);
}
}
End…...