Arrays Introducing Arrays Declaring Array Variables Creating Arrays, and Initializing Arrays Copying Arrays Multidimensional Arrays.
Download
Report
Transcript Arrays Introducing Arrays Declaring Array Variables Creating Arrays, and Initializing Arrays Copying Arrays Multidimensional Arrays.
Arrays
Introducing Arrays
Declaring Array
Variables
Creating Arrays, and Initializing Arrays
Copying Arrays
Multidimensional Arrays
Introducing Arrays
Array is a data structure that represents a collection of the
same types of data. Java treats these arrays as objects.
double[] myList = new double[10];
myList
reference
myList[0]
myList[1]
myList[2]
myList[3]
myList[4]
myList[5]
myList[6]
myList[7]
myList[8]
myList[9]
An Array of 10
Elements
of type double
Declaring Array Variables
datatype[] arrayname;
Example:
int[] myList;
datatype arrayname[];
Example:
int myList[];
Creating Arrays
arrayName = new datatype[arraySize];
Example:
myList = new double[10];
Declaring and Creating in One Step
datatype[] arrayname = new
datatype[arraySize];
double[] myList = new double[10];
datatype arrayname[] = new
datatype[arraySize];
double myList[] = new double[10];
Initializing Arrays
Using
a loop:
for (int i = 0; i < myList.length; i++)
myList[i] = (double)i;
Declaring,
creating, initializing in one step:
double[] myList = {1.9, 2.9, 3.4, 3.5};
Passing Arrays to Methods
Java uses pass by value to pass parameters to a method.
There are important differences between passing a value of
variables of primitive data types and passing arrays.
For a parameter of a primitive type value, the actual value
is passed. Changing the value of the local parameter inside
the method does not affect the value of the variable outside
the method.
For a parameter of an array type, the value of the
parameter contains a reference to an array; this reference is
passed to the method. Any changes to the array that occur
inside the method body will affect the original array that
was passed as the argument.
Copying Arrays?
Before the assignment
list2 = list1;
list1
After the assignment
list2 = list1;
Contents
of list1
list2
list1
Contents
of list1
list2
Contents
of list2
Contents
of list2
Copying Arrays
Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new
int[sourceArray.length];
for (int i = 0; i < sourceArrays.length; i++)
targetArray[i] = sourceArray[i];
The arraycopy Utility
arraycopy(sourceArray, src_pos,
targetArray, tar_pos, length);
Example:
System.arraycopy(A1, 0, A2, 0, A1.length);
Multidimensional Arrays
int[][] matrix = new int[10][10];
or
int matrix[][] = new int[10][10];
int[][][] matrix = new int[10][20][30];
for (int i=0; i<matrix.length; i++)
for (int j=0; j<matrix[i].length; j++)
{
matrix[i][j] = (int)(Math.random()*1000);
}
Ragged Arrays
Each row in a two-dimensional array is
itself an array. So, the rows can have
different lengths. Such an array is
known as a ragged array. For example,
int[][] matrix =
{
{1, 2, 3, 4, 5},
{2, 3, 4, 5},
{3, 4, 5},
{4, 5},
{5}
};
Array of arrays
int twoD[][] = new int[4][];
twoD[0] = new int[4];
twoD[1] = new int[3];
twoD[2] = new int[2];
twoD[3] = new int[1];
Array of Objects
Circle[] circleArray = new Circle[10];
An array of objects is actually an array of reference
variables.
So invoking circleArray[1].findArea()
involves two levels of referencing as shown in the next
figure.
circleArray references to the entire array.
circleArray[1] references to a Circle object.
Array of Objects, cont.
• Invoking circleArray[1].findArea() involves two
levels of referencing as shown in the next figure.
.
circleArray
reference
circleArray[0]
circleArray[1]
Circle object 0
…
Circle object 1
circleArray[9]
Circle object 9
circleArray references to the entire array.
circleArray[1] references to a Circle object
Summarizing the areas of the circles
// Declaration and creation
Circle[] circleArray = new Circle[10];
for(int k=0; k < circleArray.length; k++) {
circleArray[k] = new Circle(Math.random*100);
}
// Print radius and calculate the total area
double totalArea = 0;
for(k=0; k < circleArray.length; k++) {
System.out.println(“k=“+k+”) R=“+circleArray[k].getRadius());
totalArea += circleArray[k].findArea();
}
System.out.println(“Area=”+ totalArea);