Lesson 6 Arrays and Array List Classes
Lesson 6 Arrays and Array List Classes
Introduction to Arrays
Processing Array Contents
Passing Arrays as Arguments to Methods
Some Useful Array Algorithms and Operations
Returning Arrays from Methods
String Arrays
Arrays of Objects
Chapter Topics (2 of 2)
• The next step creates the array and assigns its address to the
numbers variable.
Creating Arrays (2 of 3)
Example: SameArray.java
Copying Arrays (2 of 2)
• You cannot copy an array by merely assigning one reference
variable to another.
• You need to copy the individual elements of one array to
another.
Example: PassArray.java
Comparing Arrays
Example: MonthDays.java
7.6 String Arrays (2 of 3)
• The two sets of brackets in the data type indicate that the scores
variable will reference a two-dimensional array.
• Notice that each size declarator is enclosed in its own set of
brackets.
Accessing Two-Dimensional Array
Elements
• Java automatically creates the array and fills its elements with
the initialization values.
– row 0 {1, 2, 3}
– row 1 {4, 5, 6}
– row 2 {7, 8, 9}
• Declares an array with three rows and three columns.
The length Field
• Two-dimensional arrays are arrays of one-dimensional arrays.
• To access the length fields of two dimensional array:
• The length field of the array gives the number of rows in the array and
each row has a length constant telling how many columns are there in
that row. Each row can have a different number of columns.
Summing the Elements of a Two-Dimensional
Array
• Example: CommandLine.java
• It is not required that the name of main’s parameter array
be args.
Variable-Length Argument Lists
int result;
result = sum(10, 20);
result = sum(10, 20, 30, 40);
result = sum(10, 20, 30, 40, 50, 60);
//All will work fine.
7.14 The Arraylist Class
See: ArrayListDemo6.java
Using an ArrayList