CSE215 Chapter 7 Single-Dim-Arrays
CSE215 Chapter 7 Single-Dim-Arrays
Single-Dimensional Arrays
1
Arrays
Array is a data structure that represents a collection of
same-types data elements.
String courseNumber;
courseNumber = new String ("CS 1301");
2
Declaring Array Variables
datatype[] arrayRefVar;
Example:
double[] myList; //vs. double myList;
int[] yourList;
boolean[] herList;
char[] hisList;
3
Creating Arrays
This is to allocate memory space for the array.
arrayRefVar = new datatype[arraySize];
Example:
myList = new double[10];
yourList = new int[100];
herList = new boolean[20];
hisList = new char[500];
For example,
int length = myList.length; // returns 10
int length = yourList.length; // returns 100;
int length = herList.length; // returns 20;
int length = hisList.length; // returns 500;
5
Array Representation
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
6
Default Values
When an array is created, its elements are assigned the default value
of
7
Indexed Variables
The array elements are accessed through the index.
The array indices start from 0 to arrayRefVar.length-1
Example:
myList = new double[5];
Array myList holds five double values
The indices are 0 to 4.
Each element in the array is represented using the following syntax,
known as an indexed variable:
arrayRefVar[index];
8
Array Initialization
Declaring, creating, initializing in one step:
double[] myList = {1.9, 2.9, 3.4, 3.5};
int[] numberGrades = {70, 65, 87, 93, 90};
char[] letterGrades = {'C', 'D', 'B', 'A', 'A'};
Boolean[] myFlags = {true, false, true, false};
10
Initializing arrays with input values
11
Initializing arrays with random values
12
Printing arrays
... //Some code
13
Summing all elements
double total = 0;
14
Finding the largest element
... //Some code
15
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.
16
Copying Arrays
For object references, an assignment statement copies the
memory address NOT the array content:
name2 = name1;
17
Copying Arrays
Use a loop:
18
The arraycopy Utility
arraycopy(sourceArray, src_pos, targetArray, tar_pos, length);
Example:
19
Passing Arrays to Methods
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
21
Pass By Value - Revisited
Java uses pass by value to pass arguments 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.
22
Simple Example
public class Test {
public static void main(String[] args)
{
int x = 1; // x represents an int value
int[] y = new int[10]; // y is an array of int values
myMethod(x, y); // Invoke m with arguments x and y
24
Call (Run-Time) Stack
Stack Heap
Space required for
method myMethod
int[] numbers:reference
The arrays are
int number: 1001 5555 stored in a
0 heap.
Space required for the
main method
int[] y: reference Array of ten int
int x: 1 values is stored here
0
25
Call (Run-Time) Stack
Heap
26
Variable-Length Parameter List
Java allows a method to take variables number of parameters of the
same type. The parameter list is treated as an array.
28
Class Arrays
Since binary search is frequently used in programming, Java provides
several overloaded binarySearch methods for searching a key in an array of
int, double, char, short, long, and float in the java.util.Arrays class. For
example:
import java.util.*;
int[] list = {2,4,7,10,11,45,50,59,60,66,69,70,79};
System.out.println("Index of value 11 is " +
Arrays.binarySearch(list, 11));
30
Arrays.toString(list) Method
Method Arrays.toString(list) method can be used to return a string
representation for the list.
import java.util.*;
char[] chars = {'a', 'A', '4', 'F', 'D', 'P'};
Arrays.sort(chars); //sort the list
//print the list
for (int i = 0; i < chars.length; i++)
System.out.print(chars[i]+ " ");
System.out.println();
//convert to string
String myString = Arrays.toString(chars);
System.out.println (myString);
Output:
4 A D F P a
[4, A, D, F, P, a]
31
Passing Arguments to Main Method
You can call a regular method by passing actual parameters. Can
you pass arguments to main? Of course, yes. For example, the main
method in class B is invoked by a method in A, as shown below:
32
End of Chapter 7
33