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

Arrays Unit 3

The document discusses one-dimensional and two-dimensional arrays in Java. It explains that a one-dimensional array uses a single subscript to store a list of items under one variable name. A two-dimensional array can be used to store a table of values using two indices. It also covers declaring and initializing arrays, including creating array memory, assigning values to elements, and accessing the length of an array.

Uploaded by

Tamil views
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)
6 views

Arrays Unit 3

The document discusses one-dimensional and two-dimensional arrays in Java. It explains that a one-dimensional array uses a single subscript to store a list of items under one variable name. A two-dimensional array can be used to store a table of values using two indices. It also covers declaring and initializing arrays, including creating array memory, assigning values to elements, and accessing the length of an array.

Uploaded by

Tamil views
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/ 2

The individual values are called elements.

ONE- DIMENSIONAL ARRAYS

$ Alist of items can be given one variable name using only one subscript called single subscripted
variable or one dimensional array.
Eg: int number[]=new int[5):
For example, The values to the array elements 35, 40, 20, 57, 19 can beassigned as follows:
number[0] = 35;

number[1] =40;

number[2] = 20;

number[3] =57;

number[4] = 19;

CREATING AN ARRAY

$ Arrays must be declared and created in the computer memory before they are used.
Creation of an array involves three steps:
Declaring the array
Creating memory locations
Putting values into the memory locations.

Declaration of arrays

Creating memory locations

After declaring an array, we need to create memory.

4 Java allows creating arrays using new operator.

arrayname = new type [size];

Eg: number-new int[S]:


Combining declaration and creation

It is also possible to combine the two steps- declaration and creation


" Int number|=new int|5];

Initialization of Arrays
Putting values into the array is known as initialization.

This process is done using the array subscript.


arrayname (subseriptj=value;
Eg: numberl0|-90;

number[ 1]-100;

< Arrays starts with a subscript Oand ends with avalue one les than the size specified.
Trying to access an array beyond its boundaries will generate an error message.
Initializing list of values to an array

* Values are separated by commas and surrounded by curly braces.

type arrayname||={ list of values}:


4 If size is not specified means compiler allocates enough space for all the elements specified in
the list

Eg int numberl|-{23,45,56,67}
Array length
Access the length of the array using length keyword.

Eg int assize= a.length0;

Eg: int sizel||l=new int|3||10|:

> Here length of size.length() 3

> Length of size{i].length() 10,

TWO DIMENSIONALARRAYS

By using two dimensional arrays we can store table of values.

Given: int twoD [] [] = new int (4] [51:

Eg : int myarraylll:

myarray=new int|3||41;

Or int myarray[|l|=new int|3||4]

Example program:
class MulTable

final static int ROWS = 0:

final static int columns = 20:

public static void main(String args[ ])

int product [])=new int [ ROWS ) [COLUMNS]:


int row, column;

System.out.printin(" MULTIPLICATION TABLE");


System.out.printin(" ):
int ij:
for ( i =10; i< ROWS; i+)

for (j=10: j <COLUMNS; j++)

product [i] LÜ] =i:


System.out.println(" +product [ i] [i ):

System.out.printin( «):
}

You might also like