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

Class 3++++

The document provides an overview of arrays in Java, explaining how to create and manipulate one-dimensional and two-dimensional arrays. It covers array declaration, initialization, accessing elements, and the importance of array size, along with examples and tasks for practical application. Additionally, it highlights common errors such as ArrayIndexOutOfBoundException when accessing invalid indices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views19 pages

Class 3++++

The document provides an overview of arrays in Java, explaining how to create and manipulate one-dimensional and two-dimensional arrays. It covers array declaration, initialization, accessing elements, and the importance of array size, along with examples and tasks for practical application. Additionally, it highlights common errors such as ArrayIndexOutOfBoundException when accessing invalid indices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Class 3

Array
Two-dimensional array
Tablicy (array)
● An array is a special object in which you can store
not one value, but multiple values.
● In such an object there are several cells, each of
which can be accessed by its number (index)
starting from zero.
● To do this, after the name of the array variable in
square brackets, you need to specify the index of
the cell to which we access:
array[index] = value;
array - name of the array variable
index - cell number in the array
value - the value that we want to put in a specific
cell
Creating an array of elements in Java
If we want to store 100 integers, we need an
array of type int:
int[] array = new int[100];
Declaration of a array variable with type int. The
int type is followed by square brackets [], which
indicates that you can store not one value, but
multiple values. On the right side of the “=” sign
we have written “new” for creating array of 100
elements of type int.
Create an array of 20 elements to store real
(double) numbers:
double[] vals = new double[20];
The number of cells in an array is called
the array size or array length. And
because of the ability to store multiple
values, arrays are also called
containers.

Important: the size of the array cannot


be changed after creation. You can
create a new one, but you cannot
change the length of an already created
array.
The numbering of the cells of the array
always starts from zero. If we have an
array of 10 elements, then the numbers
(indexes) of its cells are 0..9, if the array
has 200 elements, then 0..199.
Code Explanation

int[] a = new int[10]; We create an array of 10 elements of type


int.
a[2] = 4; In the cell with index 2 we write the value
4.
a[7] = 9; In the cell with index 7 we write the value
9.
a[9] = a[2] + a[5]; In a cell with index 9, we write the sum of
the values stored in cells 2 (stored 4) and
5 (stored 0).
Code Explanation
int[] a = new int[10]; We create an array of 10 elements of
type int.
a[2] = 4; In the cell with index 2 we write the
value 4.
a[7] = 9; In the cell with index 7 we write the
value 9.
int[] b = a; In variable b, we write the address that
is in variable a.
Now a and b are pointing to the same
array object in a memory.
a[9] = b[2] + a[7]; In the index cell 9 of the array object, we
write the sum of the values stored in
cells 2 (stored 4) and 7 (stored 9).
Code Explanation
int n = 100; Create an array of N elements
int[] a = new int[n];
int n = 100; Create an array of 203 elements
int[] a = new int[n * 2 + 3];
Important:
Note that if you try to refer to an array
cell by an index that is not in the array (in
our case, all integers except 0..99), the
program will crash with
ArrayIndexOutOfBoundException. It
means that index is outside of the
bounds of the array.
Code Explanation
int[] array; We create a variable - an array of type
if (a < 10)
array = new int[10]; int [ ]
else If variable a is less than 10,
array = new int[20]; then create an array of 10 elements.
for (int i = 0; i < array.length; i++)
{ Otherwise we create an array of 20
System.out.println(array[i]); elements
} Loop on all elements of the array: from 0
to the length of the array.length — 1
Filling an array of 10 numbers with numbers from 0 to 9:
int[] array = new int[10]; We create an object - an array of 10
elements
for (int i = 0; i < 10; i++) Cycle from 0 to 9 inclusive
{
array[i] = i; In the cells we put values from 0 to 9
}
Fast array initialization in Java

int[] months = new int[] { 31, 28,


31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
Two dimensional arrays
int[][] array = new int[width][wheight];
array is the name of 2-dimensional array variable
width is the width of the table (in cells)
height is the height of the table

Code Explanation

int[][] data = new int[2][5];


Creating a two-dimensional array with
data[1][1] = 5; two columns and 5 rows.
In cell (1,1) we write 5.
Fast two-dimensional array initialization:

int[][] months = { {31, 28, 31},


{30, 31, 30}, {31, 31, 30}, {31,
30, 31} };
int[][] data = new int[2][5];
int[] row1 = data[0];
int[] row2 = data[1];

Code Explanation
In matrix [0] we store the reference to
the first row.
int[][] matrix = {

{1, 2, 3, 4, 5},

{5, 4, 3, 2, 1} We swap links.


}; As the result the matrix looks like the
int[] tmp = matrix[0]; following:
{
matrix[0] = matrix[1]; {5, 4, 3, 2, 1},
{1, 2, 3, 4, 5}
matrix[1] = tmp; };
Task 0
1. Create an array of 10 integers.
2. Enter 10 numbers from the keyboard and save them in
the array.
3. Find the minimum number in this array
4. Display the minimum on the screen.
Task 1
1. Create an array of 10 integers.
2. Enter 10 numbers from the keyboard and save them in
the array.
3. Display the array items in reverse order
4. Display each value from the new line.
Requirements:
* The program must create an array of 10 integers.
* The program must read the numbers for the array from
the keyboard.
* The program should output 10 lines, each with a new
line.
* The array must be displayed in reverse order.
Task 2
1. Create an array of 20 numbers.
2. Enter values for it from the keyboard.
3. Create two arrays of 10 numbers each.
4. Copy the large array into two small ones: half the numbers to
the first small one, the other half to the second small one.
5. Display a second small array on the screen, each value is
output from a new line.
Requirements:
* The program should create a large array of 20 integers.
* The program must read from the keyboard 20 numbers for a
large array.
* The program must create two small arrays of 10 numbers each.
* The program must copy half of the large array to the first small
one and the other to the other and display the second small array
on the screen.
Task 3
1. Create an array of type String of 10 elements.
2. Create an array of type int of 10 elements.
3. Enter 10 lines from the keyboard, put them into created string
array.
4. In each cell of the number array, write the length of the row from
the row array whose index / cell number corresponds to the current
index from the number array.

Display the contents of the array of numbers on the screen, display


each value from a new line.
Requirements:
* The program must create an array of 10 String lines.
* The program must create an array of 10 integers.
* The program should read the rows of the array from the keyboard.
* The program must write in an array of numbers the length of the
rows from the array of rows, and then display them on the screen.

You might also like