0% found this document useful (0 votes)
2 views19 pages

Java Arrays

The document provides an overview of arrays in Java, including their declaration, initialization, and access methods. It explains how to create single and multidimensional arrays, as well as coding guidelines and examples for manipulating array elements. Additionally, it highlights the importance of array length and the use of indices for accessing individual elements.

Uploaded by

julius.claour
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
2 views19 pages

Java Arrays

The document provides an overview of arrays in Java, including their declaration, initialization, and access methods. It explains how to create single and multidimensional arrays, as well as coding guidelines and examples for manipulating array elements. Additionally, it highlights the importance of array length and the use of indices for accessing individual elements.

Uploaded by

julius.claour
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 19

Arrays

• Suppose there are three variables of type int with different identifiers for
each variable.

int number1;
int number2;
int number 3;
number1 = 1;
number2 = 2;
number3 = 3;

• This seems like a tedious task in order to just initialize and use the
variables especially if they are used for the same purpose.
Arrays
• An array is a sequence of memory locations for storing data set
out in such a way that any one of the individual locations can be
accessed by quoting its index number.

0 1 2
number: 1 2 3
• The individual items within an array are called elements.
Declaring Java Arrays
• To declare an array, write the data type, followed by a set of square brackets [], followed by the
identifier name.

ElementType[] arrayName;

Example:

int[] x;
int[] ages;

Or equivalently

int x[];
int ages[];
Initializing Array Variables

• To initialize an array, you use the new operator to allocate memory for
objects. The new operator is followed by the data type with the number of
elements to allocate specified. The number of elements to be allocated is
placed within the [] operator.

Example:

name = new String[100];


age = new int[10];
Initializing Array Variables

• Memory model for array storage


:
x
:
x[0]
x[1]
x[2]
x[3]
: :
Initializing Array Variables

• Like other variables, an array variable can be initialized when it is declared.

ElementType[] arrayName = new ElementType[sizeOfArray];

or

ElementType arrayName[] = new ElementType[sizeOfArray];

Example:

int[] x = new int[10];


int[] ages = new int[100];
Initializing Array Variables

• An array can be also created by directly initializing it with data.

Example:

int[] arr = {1, 2, 3, 4, 5};

• This statement declares and creates an array of integers with


five elements, and initializes this with the values 1, 2, 3, 4, and
5.
Examples
• Creates an array of Boolean variables with identifier results. This array contains 4
elements that are initialized to values {true, false, true, false}.

• boolean[] results = {true, false, true, false};

• Creates an array of 4 double variables initialized to the values {100, 90, 80, 75}.

• double[] grades = {100, 90, 80, 75};

• Creates an array of strings with identifier days and initialized. This array contains 7
elements.

• String[] days = {“Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun”};


Exercise
• Declare and create array variables for the
following data:

• An array of 15 doubles
• An array of 20 strings
Accessing an Array Element

Example:

//assigns 10 to the first element in the array


ages [0] = 10;

//prints the last element in the array


System.out.print(ages[99]);
Accessing an Array Element

• To access an array element, or a part of the array, use a


number called an index or a subscript.

• index number or subscript


• assigned to each member of the array, to allow the program
to access an individual member of the array
• an integer beginning at zero and progresses sequentially by
whole numbers to the end of the array
• Note: index is from 0 to (sizeOfArray-1)
Accessing an Array Element

• The example below illustrates how to print all the elements in


the array.

public class ArraySample {


public static void main(String[] args) {
int[] ages = new int[100];
for(int i=0; i<100; i++) {
System.out.print(ages[i]);
}
}
}
Coding Guidelines
1.It is better to create the array right away after you declare it.
• int[] arr = new int[100];

2.The elements of an n-element array have indexes from 0 to n-1.


Note that there is no array element arr[n]! This will result in an
array-index-out-of-bounds exception.

3.You cannot resize an array. However, you can create a new array
of a different size, copy the desired data into the new array, and
assign the new array to the array variable.
Array Length
• All array indices begin at 0. The number of elements in an array
is stored as part of the array object in the length attribute.

• Use the length attribute to iterate on an array as follows:

int list[] = new int[10];


for(int i = 0; i < list. length; i++)
{
System.out.println(list[i]);
}
Example of Array Manipulation
/** Array manipulation */
public class upavon
{
public static void main(String[] args) {
int [] sizes = {31,28,31,30,31,30,31,31,30,31,30,31};
System.out.print("number ... ");
int month = (int) wellreader.read_number();

if (month < 1 || month > 12)


{
System.out.println("Not a valid month number");
} else {
System.out.print("That month has ");
System.out.print(sizes[month-1]);
System.out.println(" days");
}

System.out.print("Length of sizes array: ");


System.out.println(sizes.length);

int day = month;


sizes = new int[7];
for (int k=0;k<7;k++) sizes[k]=24;
if (day < 1 || day > 7)
{
System.out.println("Not a valid day number");
} else {
System.out.print("That day has ");
System.out.print(sizes[day-1]);
System.out.println(" hours");
}

System.out.print("Length of sizes array: ");


System.out.println(sizes.length);
}
}
Multidimensional Arrays

• Multidimensional arrays are implemented as arrays of


arrays.

• Multidimensional arrays are declared by appending the


appropriate number of bracket pairs after the array
name.
Multidimensional Arrays

Example:
• //integer array 512 x 128 elements
int[][] twoD = new int[512][128];

• //character array 8 x 16
char[][] twoD = new char[8][16];

• //String array 4 rows x 2 columns


String[][] dogs = {{“terry”, “brown”},
{“kristin”, “white”},
{“toby”, “gray”},
{“fido”, “black”}
};
Multidimensional Arrays

• Accessing an element in a multidimensional array is the same as


accessing elements in a one dimensional array.

Example:

• To access the first element in the first row of the array dogs, we
write,

System.out.print(dogs[0][0]);

This will print the String “terry” on the screen.


Example
int[][] matrix = {{0, 1, 2, 3}, {1, 0, 3, 2}, {2, 3, 0, 1}, {3, 2,
1, 0}};
int row, col;
for(row = 0; row < 4; row++) {
for(col = 0; col < 4; col++) {
System.out.print(“ ” + matrix[row]
[col]);
System.out.println();
}
}

You might also like