DEV Community

Abdelrhman khaled
Abdelrhman khaled

Posted on

1D Array Revision by java

Quickly, we will recap about 1D array and do some iterations on it

Declaration

DataType nameOfArray [] = new DataType [lengthOfArray];

Example
int array [] = new int [5];

If I want to access an element in array

ArrayName[index]

array[2];

Note that arrays is zero based:

Array of length 5
The 1st element in array[0]
The last element array[lengthOfArray-1] = array[4]

Example

int array [] = new int [5];
array [0] = 7;
array [1] = 23;
array [2] = 20;
array [3] = 17;
array [4] = 45;
Enter fullscreen mode Exit fullscreen mode

System.out.println( array[0] ); // will print 7

looping on 1D array

For (int i=0 ; i<array.length();i++)
{
System.out.println(array[i] ) ;
}
Enter fullscreen mode Exit fullscreen mode

Great questions need some one to solve

1st and easy question

Image description

2nd question and easy too

Image description

Image description

Image description

last one but is medium !

Image description

Image description

Top comments (0)