0% found this document useful (0 votes)
50 views

INF 208 Programming - Java: Arrays

This document provides an overview of arrays in Java, including how to declare and initialize arrays, access array elements, loop through arrays, pass arrays as parameters and return arrays from methods. Some key points covered are: - Arrays allow storing multiple values of the same type and can be accessed via an index. - Arrays are declared with the type, [], and new, specifying the length. - Elements are accessed and modified using name[index]. - for loops are commonly used to iterate through arrays. - Arrays are passed by reference, so changes within methods also change the original array. - Methods can return arrays by declaring the return type as the array type.

Uploaded by

Ashiru katto
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

INF 208 Programming - Java: Arrays

This document provides an overview of arrays in Java, including how to declare and initialize arrays, access array elements, loop through arrays, pass arrays as parameters and return arrays from methods. Some key points covered are: - Arrays allow storing multiple values of the same type and can be accessed via an index. - Arrays are declared with the type, [], and new, specifying the length. - Elements are accessed and modified using name[index]. - for loops are commonly used to iterate through arrays. - Arrays are passed by reference, so changes within methods also change the original array. - Methods can return arrays by declaring the return type as the array type.

Uploaded by

Ashiru katto
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

INF 208 Programming - Java

Arrays
Arrays
 array: object that stores many values of the same
type.
• element: One value in an array.
• index: A 0-based integer to access an element from an array.

index 0 1 2 3 4 5 6 7 8 9

value 12 49 -2 26 5 17 -6 84 72 3

element 0 element 4 element 9


 Array declaration
type[] name = new type[length];
• Example
int[] numbers = new int[10];
Array declaration

 The length can be any integer expression.


int x = 2 * 3 + 1;
int[] data = new int[x % 5 + 2];
 Each element initially gets a "zero-equivalent" value.

Type Default value


int 0
double 0.0
boolean false
String null
or other (means, "no
object object")
Accessing elements

 Accessing and modifying array elements


name[index] // access
name[index] = value; // modify
 Example:
numbers[0] = 27;
numbers[3] = -6;

System.out.println(numbers[0]);
if (numbers[3] < 0) {
System.out.println("Element 3 is negative");
}

index 0 1 2 3 4 5 6 7 8 9

value 27
0 0 0 -6
0 0 0 0 0 0 0
Arrays of other types
double[] results = new double[5];
results[2] = 3.4;
results[4] = -0.5;

index 0 1 2 3 4
value 0.0 0.0 3.4 0.0 -0.5

boolean[] tests = new boolean[6];


tests[3] = true;

index 0 1 2 3 4 5
value false false false true false false
Out-of-bounds

 Legal indexes: between 0 and the array's length - 1.


• Reading or writing any index outside this range will throw an
ArrayIndexOutOfBoundsException.
 Example:
int[] data = new int[10];
System.out.println(data[0]); // okay
System.out.println(data[9]); // okay
System.out.println(data[-1]); //
exception
System.out.println(data[10]); //
exception
index 0 1 2 3 4 5 6 7 8 9

value 0 0 0 0 0 0 0 0 0 0
Accessing array elements

int[] numbers = new int[8];


numbers[1] = 3;
numbers[4] = 99;
numbers[6] = 2;
int x = numbers[1];
numbers[x] = 42;
numbers[numbers[6]] = 11; //use numbers[6] as
index
x 3

index 0 1 2 3 4 5 6 7 8 9

value 0 3 11 42 99 0 2 0 0 0
numbers
Arrays and for loops
 It is common to use for loops to access array
elements.
for (int i = 0; i < 8; i++) {
System.out.print(numbers[i] + " ");
}
System.out.println();// output: 0 4 11 0 44 0 0 2

 Sometimes we assign each element a value in a loop.


for (int i = 0; i < 8; i++) {
numbers[i] = 2 * i;
}

index 0 1 2 3 4 5 6 7

value 0 2 4 6 8 10 12 14
The length field

 An array's length field stores its number of elements.


name.length

for (int i = 0; i < numbers.length; i++) {


System.out.print(numbers[i] + " ");
}
// output: 0 2 4 6 8 10 12 14
•It does not use parentheses like a String's .length().

 What expressions refer to:


•The last element of any array?
•The middle element?
Value semantics (primitives)

 value semantics: Behavior where values are copied


when assigned to each other or passed as
parameters.
• When one primitive variable is assigned to another,
its value is copied.
• Modifying the value of one variable does not affect others.
int x = 5;
int y = x; // x = 5, y = 5
y = 17; // x = 5, y = 17
x = 8; // x = 8, y = 17
Reference semantics (objects)
 reference semantics: Behavior where variables
actually store the address of an object in memory.
• When one reference variable is assigned to another, the
object is not copied; both variables refer to the same object.
• Modifying the value of one variable will affect others.
int[] a1 = {4, 5, 2, 12, 14, 14, 9};
int[] a2 = a1; // refer to same array as a1
a2[0] = 7;
System.out.println(a1[0]); // 7

a1 index 0 1 2 3 4 5 6

value 7
4 5 2 12 14 14 9
a2
Arrays as parameters

 Declaration:
public static type methodName(type[] name) {
Example:
public static double average(int[] numbers)
{

 Call:
methodName(arrayName);

Example:
int[] scores = {13, 17, 12, 15, 11};
double avg = average(scores);
Array parameter example

public static void main(String[] args) {


int[] iq = {126, 84, 149, 167, 95};
double avg = average(iq);
System.out.println("Average = " + avg);
}

public static double average(int[] array) {


int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return (double) sum / array.length;
}

Output:
Average = 124.2
Arrays passed by reference

 Arrays are objects.


• When passed as parameters, they are passed by reference.
(Changes made in the method are also seen by the caller.)
 Example:
public static void main(String[] args) {
int[] iq = {126, 167, 95}; iq
doubleAll(iq);
System.out.println(Arrays.toString(iq));
}
public static void doubleAll(int[] a) {
for (int i = 0; i < a.length; i++) {
a[i] = a[i] * 2;
}
}
index 0 1 2
• Output:
a value 126
252 167
334 190
95
[252, 334, 190]
Arrays as return (declaring)

public static type[] methodName(parameters) {

• Example:

public static int[] countDigits(int n) {


int[] counts = new int[10];
while (n > 0) {
int digit = n % 10;
n = n / 10;
counts[digit]++;
}
return counts;
}
Arrays as return (calling)

type[] name = methodName(parameters);

• Example:

public static void main(String[] args) {


int[] tally = countDigits(229231007);
System.out.println(Arrays.toString(tally));
}

Output:

[2, 1, 3, 1, 0, 0, 0, 1, 0, 1]
Reading: 7.1-7.4
Self-Check Problems pg 505: 1-11, 13-17
Exercises pg 511: 1-4

You might also like