CSE211 Lecture 5
CSE211 Lecture 5
Introducing Arrays
Array is a data structure that represents a collection of the
same types of data.
double[] myList = new double[10];
myList reference
myList[0] 5.6
myList[1] 4.5
Array reference myList[2] 3.3
variable
myList[3] 13.2
myList[4] 4
Array element at
myList[5] 34.33 Element value
index 5
myList[6] 34
myList[7] 45.45
myList[8] 99.993
myList[9] 11123
2
Arrays
The general form of a one dimensional array declaration is:
dataType[] arr; (or)
dataType []arr; (or)
dataType arr[];
arrayRefVar.length
For example,
month_days.length returns 12
5
Indexed Variables
The array elements are accessed through the index. The
array indices are 0-based, i.e., it starts from 0 to
arrayRefVar.length-1. In the example month_days
holds twelve int values and the indices are from 0 to
11.
arrayRefVar[index];
6
Using Indexed Variables
After an array is created, an indexed variable can
be used in the same way as a regular variable.
For example, the following code adds the value
in month_days[0] and month_days[1] to
month_days[2].
7
Array Initializers
Declaring, creating, initializing in one step:
double[] myList = {1.9, 2.9, 3.4, 3.5};
8
Declaring, creating, initializing
Using the Shorthand Notation
double[] myList = {1.9, 2.9, 3.4, 3.5};
9
CAUTION
Using the shorthand notation, you
have to declare, create, and initialize
the array all in one statement.
Splitting it would cause a syntax
error. For example, the following is
wrong:
double[] myList;
12
Finding the largest element
double max = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > max) max = a[i];
}
13
Enhanced for Loop (for-each loop)
JDK 1.5 introduced a new for loop that enables you to traverse the complete array
sequentially without using an index variable. For example, the following code
displays all elements in the array myList:
You still have to use an index variable if you wish to traverse the array in a
different order or change the elements in the array.
14
Copying Arrays
Often, in a program, you need to duplicate an array or a part of an
array. In such cases you could attempt to use the assignment statement
(=), as follows:
list2 = list1;
Before the assignment After the assignment
list2 = list1; list2 = list1;
list1 list1
Contents Contents
of list1 of list1
list2 list2
Contents Contents
of list2 of list2
Garbage
15
Copying Arrays
Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new
int[sourceArray.length];
16
The arraycopy Utility
arraycopy(sourceArray, src_pos,
targetArray, tar_pos, length);
Example:
System.arraycopy(sourceArray, 0,
targetArray, 0, sourceArray.length);
17
public class Array {
import java.util.Arrays;
20
Two dimensional Arrays
Two dimensional Arrays:
In Java, two dimensional arrays are actually arrays of
arrays.
To declare a two dimensional array variable, specify each
additional index using another set of square brackets.
21
Declare/Create Two-dimensional Arrays
// Declare array ref var
int[][] refVar;
22
Multidimensional Arrays
23
Two-dimensional Array Illustration
0 1 2 3 4 0 1 2 3 4 0 1 2
0 0 0 1 2 3
1 1 1 4 5 6
2 2 7 2 7 8 9
3 3 3 10 11 12
4 4 int[][] array = {
{1, 2, 3},
matrix = new int[5][5]; matrix[2][1] = 7; {4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
matrix.length? 5 array.length? 4
matrix[0].length? 5 array[0].length? 3
24
Declaring, Creating, and Initializing Using
Shorthand Notations
You can also use an array initializer to declare, create and
initialize a two-dimensional array. For example,
25
Lengths of Two-dimensional
Arrays
int[][] x = new int[3][4];
x
x[0][0] x[0][1] x[0][2] x[0][3] x[0].length is 4
x[0]
x[1] x[1][0] x[1][1] x[1][2] x[1][3] x[1].length is 4
x[2]
x[2][0] x[2][1] x[2][2] x[2][3] x[2].length is 4
x.length is 3
26
Alternative Array Declaration Syntax
This alternative declaration form offers convenience when
declaring several arrays at the same time. For example,
int[][] triangleArray = { 1 2 3 4 5
{1, 2, 3, 4, 5},
{2, 3, 4, 5}, 1 2 3 4
{3, 4, 5},
{4, 5}, 1 2 3
{5}
}; 1 2
1 2
29
30
Processing Two-Dimensional Arrays
See the examples in the text.
1. (Initializing arrays with input values)
2. (Printing arrays)
3. (Summing all elements)
4. (Summing all elements by column)
5. (Which row has the largest sum)
6. (Finding the smallest index of the largest element)
31
Initializing arrays with input values
java.util.Scanner input = new Scanner(System.in);
System.out.println("Enter " + matrix.length + " rows and " +
matrix[0].length + " columns: ");
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
matrix[row][column] = input.nextInt();
}
}
32
Initializing arrays with random values
33
Printing arrays
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
System.out.print(matrix[row][column] + " ");
}
System.out.println();
}
34
Printing arrays using
Enhanced for loop
for (int row[] : matrix){
for (int column : row) {
System.out.print(column + " ");
}
System.out.println();
}
35
Summing all elements
int total = 0;
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
total += matrix[row][column];
}
}
36
Summing elements by column
for (int column = 0; column < matrix[0].length; column++) {
int total = 0;
for (int row = 0; row < matrix.length; row++)
total += matrix[row][column];
System.out.println("Sum for column " + column + " is "
+ total);
}
37
What is the class name of java array?
In java, array is an object. In Java, arrays inherit the getClass and getName
methods from the java.lang.Object class, which is the root of the Java class
hierarchy.
getClass().getName() method on the object.
class Testarray4{
public static void main(String args[]){
int arr[]={4,4,5};
Class c=arr.getClass();
String name=c.getName(); Output: [I
System.out.println(name);
}
}
The class name of arrays of primitive data types in Java uses a shorthand
notation, where each primitive type is represented by a single uppercase
letter. The letter corresponds to the type of the elements in the array.
38
Example 1: How to put a Scanner inpu
t into an array
39
Example 2: How to
put a Scanner inpu
t into an array
40