ARRAYS BASICS

ARRAYS:

  • An array is a collection of similar data types. [Datatype -> Primitive & Non primitive]
    • [We can create array for object class. Above line is not suitable for that. Will learn later about that].
  • Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
    • Variable -> We can store single value
    • Array -> We can store multiple values in a single variable.
  • Square brackets is the identity of arrays.

ARRAY CHARACTERISTICS:

  • Array occupies continuous memory.
  • Array have fixed memory size (Static Memory). Unused space or memory will be wasted in array.

ARRAY DECLARATION:

int [ ] a;
int a[ ];
  • int -> datatype
  • a -> variable name
  • Declaring an array doesn’t actually create the array.
  • Must use the new keyword for creating memory for array.
  • new keyword is used to create memory for object. Array is an object. So we can use new keyword.
int [] a = new int[5];
int a[] = new int[5];
  • In this syntax, 5 denotes array length.
  • Array is an object. So, it have state & behavior. Length is a state of an array.
  • We must provide array length at the time of array creation.
  • Array length must be integer.
  • Here, array length is 5, so we have 5 elements.
  • We can access the elements using index.
    • Index starts from zero.
    • Index ends at (arrayLength-1).

INITIALIZATION OF AN ARRAY:

Elements are stored in the array
  • age[0], age[1],….. age[4] –> index
  • 12, 4, 5, 2, 5 –> elements

Way 1:

//Declare array
int age[] = new int[5];

//Initialization array
	age[0] = 12;
	age[1] = 4;
	age[2] = 5;
	age[3] = 2;
	age[4] = 5;

Way 2:

int age[] = {12, 4, 5, 2, 5};

ACCESS ARRAY ELEMENTS:

Ex 1:

public class Sample {

	public static void main(String[] args) {
		
	int age[] = new int[5];
	age[0] = 12;
	age[1] = 4;
	age[2] = 5;
	age[3] = 2;
	age[4] = 5;
	
	System.out.println("First Element: " + age[0]);
	System.out.println("Second Element: " + age[1]);
	System.out.println("Third Element: " + age[2]);
	System.out.println("Fourth Element: " + age[3]);
	System.out.println("Fifth Element: " + age[4]);
	
	}
}

Output:

First Element: 12
Second Element: 4
Third Element: 5
Fourth Element: 2
Fifth Element: 5

Ex 2: Using for loop

public class Sample {

	public static void main(String[] args) {
		
	int age[] = new int[5];
	age[0] = 12;
	age[1] = 4;
	age[2] = 5;
	age[3] = 2;
	age[4] = 5;
	
	for(int i=0; i<age.length; i++)
	{
	System.out.println(age[i]);
	}
}
}

Output:

12
4
5
2
5


Basic program for finding array length:

Ex 1:

public class Sample {

	public static void main(String[] args) {
		
		int[] array1 = {12,56,90};
		int[] array2 = new int[10];
		int[] array3 = new int[0];		
		int[][] array4 = new int[2][4];
		
		System.out.println("Array1 Length: " + array1.length);
		System.out.println("Array2 Length: " + array2.length);
		System.out.println("Array3 Length: " + array3.length);
		System.out.println("Array4 Length: " + array4.length);
}
}

Output:

Array1 Length: 3
Array2 Length: 10
Array3 Length: 0
Array4 Length: 2

Ex 2:

public class Sample {

	public static void main(String[] args) 
	{		
		int[] array1 = new int[-2];
    	System.out.println("Array1 Length: " + array1.length);
    }
}

Output:

Exception in thread “main” java.lang.NegativeArraySizeException: -2

NOTE:

  • Array length must be positive integer.

If we provide array length only, Not provide values for elements. Then what did happen?

public class Demo1 {

	public static void main(String[] args) {
		Demo1 obj = new Demo1();
		obj.array_byte();
		System.out.println();
		obj.array_short();
		System.out.println();
		obj.array_int();
		System.out.println();
		obj.array_long();
		System.out.println();
		obj.array_float();
		System.out.println();
		obj.array_double();
		System.out.println();
		obj.array_boolean();
		System.out.println();
		obj.array_char();
		System.out.println();
		obj.array_string();
		System.out.println();
		obj.array_class();
		System.out.println();
	}
	
    public void array_byte() {
		byte array[] = new byte[3];
		System.out.println("Byte Array - default values are");
		for (int i = 0; i < array.length; i++) 
		{
			System.out.println(array[i]);
		}
	}
	
	public void array_short() {
		short array[] = new short[3];
		System.out.println("Short Array - default values are");
		for (int i = 0; i < array.length; i++) 
		{
			System.out.println(array[i]);
		}
	}
	
	public void array_int() {
		int array[] = new int[3];
		System.out.println("Int Array - default values are");
		for (int i = 0; i < array.length; i++) 
		{
			System.out.println(array[i]);
		}
	}
	
	public void array_long() {
		long array[] = new long[3];
		System.out.println("Long Array - default values are");
		for (int i = 0; i < array.length; i++) 
		{
			System.out.println(array[i]);
		}
	}
	
	public void array_float() {
		float array[] = new float[3];
		System.out.println("Float Array - default values are");
		for (int i = 0; i < array.length; i++) 
		{
			System.out.println(array[i]);
		}
	}	
	
	public void array_double() {
		double array[] = new double[3];
		System.out.println("Double Array - default values are");
		for (int i = 0; i < array.length; i++) 
		{
			System.out.println(array[i]);
		}
	}
	
	public void array_boolean() {
		boolean array[] = new boolean[3];
		System.out.println("Boolean Array - default values are");
		for (int i = 0; i < array.length; i++) 
		{
			System.out.println(array[i]);
		}
	}
	
	public void array_char() {
		char array[] = new char[3];
		System.out.println("Char Array - default values are");
		for (int i = 0; i < array.length; i++)
		{
			System.out.println(array[i]);
		}
	}
	
	public void array_string() {
		String array[] = new String[3];
		System.out.println("String Array - default values are");
		for (int i = 0; i < array.length; i++) 
		{
			System.out.println(array[i]);
		}
	}
	
	public void array_class() {
		Demo1 array[] = new Demo1[3];
		System.out.println("Class Array - default values are");
		for (int i = 0; i < array.length; i++) 
		{
			System.out.println(array[i]);
		}		
	}
}

Output:

Byte Array – default values are
0
0
0

Short Array – default values are
0
0
0

Int Array – default values are
0
0
0

Long Array – default values are
0
0
0

Float Array – default values are
0.0
0.0
0.0

Double Array – default values are
0.0
0.0
0.0

Boolean Array – default values are
false
false
false

Char Array – default values are
empty space
empty space
empty space

String Array – default values are
null
null
null

Class Array – default values are
null
null
null


Ex 2:

public class Demo1 {

	public static void main(String[] args) {
		Demo1 obj = new Demo1();
        obj.array1();
	}

	public void array1() {
		String a[] = new String[5];
		a[0] = "Rajalakshmi";
		a[1] = "Gowsalya";
		a[2] = "Mehra";
		a[3] = "Mani";
		
		int b[] = new int[5];
		b[3] = 80;
		b[4] = 79;

		for(int i=0; i<5; i++)
		{
		System.out.print(a[i] + " ");
		}
		
		System.out.println();
		
		for(int i=0; i<5; i++)
		{
		System.out.print(b[i] + " ");
		}
	}
}

Output:

Rajalakshmi Gowsalya Mehra Mani null
0 0 0 80 79


Basic Program using Scanner Class:

Ex 1:

import java.util.Scanner;

public class Demo1 {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter array length");
		int len = sc.nextInt();
		int array[] = new int[len];
		
		int i;
		
		System.out.println("Enter array value");
		for(i = 0; i<array.length;i++)
		{
			array[i] = sc.nextInt();			
		}
		
		System.out.println("Array variables are");
		for(i = 0; i<array.length;i++)
		{
			System.out.println(array[i]);			
		}
	}
}

Output:

Enter array length
4
Enter array value
10
20
30
40
Array variables are
10
20
30
40


Ex 2:

import java.util.Scanner;

public class Demo1 {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter array length");
		int len = sc.nextInt();
		int array[] = new int[len];
		
		int i;
		
		System.out.println("Enter array value");
		for(i = 0; i<array.length;i++)
		{
			array[i] = sc.nextInt();			
		}
		
		System.out.println("Array variables are");
		for(i = 0; i<array.length;i++)
		{
			System.out.println(array);			
		}
		
	}
}

Output:

Enter array length
4
Enter array value
10
20
30
40
String representation of hashcode are
[I@511baa65
[I@511baa65
[I@511baa65
[I@511baa65


Ex 3:

import java.util.Scanner;

public class Demo1 {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter array length");
		int len = sc.nextInt();
		int array[] = new int[len];
		
		int i;
		
		System.out.println("Enter array value");
		for(i = 0; i<array.length;i++)
		{
			array[i] = sc.nextInt();			
		}
		
		System.out.println("Array index are");
		for(i = 0; i<array.length;i++)
		{
			System.out.println(i);			
		}
	}
}

Output:

Enter array length
4
Enter array value
10
20
30
40
Array index are
0
1
2
3


Code Explanation:

  • In Ex 1,
    • System.out.println(array[i]);
    • array[i] ==> print array variables
  • In Ex 2,
    • System.out.println(array);
    • array ==> print String representation of hashcode
    • Click toString to know more about this.
  • In Ex 3,
    • System.out.println(i);
    • i ==> print array index

Print reverse array:

import java.util.Scanner;

public class Demo1 {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter array length");
		int len = sc.nextInt();
		int array[] = new int[len];
		int i;
		
		System.out.println("Enter array value");
		for(i=0;i<array.length;i++)
		{
			array[i] = sc.nextInt();
		}
		
		System.out.println("Array values are");
		for(i=0;i<array.length;i++)
		{
			System.out.println(array[i]);
		}
		
		System.out.println("Reverse value of array");
		for(i=(array.length-1);i>=0;i--)
		{
			System.out.println(array[i]);
		}
	}
}

Output:

Enter array length
4
Enter array value
10
20
30
40
Array values are
10
20
30
40
Reverse value of array
40
30
20
10


Find total & average:

import java.util.Scanner;

public class Demo1 {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter array length");
		int len = sc.nextInt();
		int array[] = new int[len];
		int i;
		int total = 0;
		
		System.out.println("Enter array values");
		for(i = 0;i<array.length;i++)
		{
			array[i] = sc.nextInt();
			total = total + array[i];
		}
		
		System.out.println("Array values are");
		for(i=0;i<array.length;i++)
		{
			System.out.println(array[i]);
		}
		
		System.out.println("Total: " + total);
		System.out.println("Average: " + (total/array.length));
		
	}
}

Output:

Enter array length
4
Enter array values
15
25
98
67
Array values are
15
25
98
67
Total: 205
Average: 51


Find highest, lowest value & search particular integer value:

import java.util.Scanner;

public class Demo1 {
	
	Scanner sc = new Scanner(System.in);
	static
	{
		System.out.println("Enter array length");
	}
	int len = sc.nextInt();
	int array[] = new int[len];
	int i;
	
	public static void main(String[] args)
	{
		Demo1 demo = new Demo1();
		demo.assignValue();
		demo.displayValue();
		demo.highValue();
		demo.lowValue();
		demo.divisorValue();
		demo.searchValue();
		demo.searchValue();		
	}
	
	public void assignValue()
	{
		System.out.println("Enter array values");
		for(i=0;i<array.length;i++)
		{
		array[i] = sc.nextInt();
		}
	}
	
	public void displayValue()
	{
		System.out.println("Display array values");
		for(i=0;i<array.length;i++)
		{
		System.out.println(array[i]);
		}
	}
	
	public void highValue()
	{
		int high = 0;
		for(i=0;i<array.length;i++)
		{
			if(high<array[i])
			{
				high=array[i];
			}
		}
		
		System.out.println("Highest value element: " + high);
				
	}
	
	public void lowValue()
	{
		int low = array[0];
		
		for(i=1;i<array.length;i++)
		{
			if(low>array[i])
			{
				low=array[i];
			}
		}
		
		System.out.println("Lowest value element: " + low);
	}
	
	public void divisorValue()
	{
		System.out.println("Enter any number for check that number divide any array element");
		int divisor = sc.nextInt();
		
		for(i=0;i<array.length;i++)
		{
			if(array[i]%divisor==0)
			{
				System.out.println(divisor + ": This Number can divide the following array element " + array[i]);
			}
		}
	}
	
	public void searchValue()
	{
		System.out.println("Enter the search value");
		int search = sc.nextInt();
		boolean gotValue = false;
		
		for(i=0;i<array.length;i++)
		{
			if(search==array[i])
			{
				System.out.println("Search value is present");
			    gotValue= true;
				break;
			}
		}
			if(gotValue == false)
			{
				System.out.println("Search value is not present");
			}	
	}	
}

Output:

Enter array length
4
Enter array values
12
36
89
78
Display array values
12
36
89
78
Highest value element: 89
Lowest value element: 12
Enter any number for check that number divide any array element
3
3: This Number can divide the following array element 12
3: This Number can divide the following array element 36
3: This Number can divide the following array element 78
Enter the search value
50
Search value is not present
Enter the search value
89
Search value is present


Search String Element:

import java.util.Scanner;

public class Demo1 {
	
	Scanner sc = new Scanner(System.in);
	static
	{
		System.out.println("Enter array length");
	}
	int len = sc.nextInt();
	String array[] = new String[len];
	int i;
	
	public static void main(String[] args)
	{
		Demo1 demo = new Demo1();
		demo.assignName();
		demo.displayName();
		demo.searchName();
		demo.searchName();		
	}
	
	public void assignName()
	{
		System.out.println("Enter names");
		for(i=0;i<array.length;i++)
		{
		array[i] = sc.next();
		}
	}
	
	public void displayName()
	{
		System.out.println("Display names");
		for(i=0;i<array.length;i++)
		{
		System.out.println(array[i]);
		}
	}
	
	public void searchName()
	{
		System.out.println("Enter the search person name");
		String search = sc.next();
		boolean gotValue = false;
		
		for(i=0;i<array.length;i++)
		{
			if(search.equals(array[i]))
			{
				System.out.println("Search person is present");
			    gotValue= true;
				break;
			}
		}
			if(gotValue == false)
			{
				System.out.println("Search person is not present");
			}	
	}	
}

Output:

Enter array length
4
Enter names
Rajii
Mani
Gowsy
Mehra
Display names
Rajii
Mani
Gowsy
Mehra
Enter the search person name
Arjun
Search person is not present
Enter the search person name
Mani
Search person is present


NOTE:

  • Primitive data types -> use == for comparision
  • Non primitive data types -> use .equals method for comparison

Find Maximum, Second maximum value:

import java.util.Scanner;

public class Demo1 {
	
	Scanner sc = new Scanner(System.in);
	static
	{
		System.out.println("Enter array length");
	}
	int len = sc.nextInt();
	int array[] = new int[len];
	int i;
	
	public static void main(String[] args)
	{
		Demo1 demo = new Demo1();
		demo.assignValue();
		demo.displayValue();
		demo.maxValue();
	}
	
	public void assignValue()
	{
		System.out.println("Enter array values");
		for(i=0;i<array.length;i++)
		{
		array[i] = sc.nextInt();
		}
	}
	
	public void displayValue()
	{
		System.out.println("Display array values");
		for(i=0;i<array.length;i++)
		{
		System.out.println(array[i]);
		}
	}
	
	public void maxValue()
	{
		int max = 0;
		int secondMax = 0;
		for(i=0;i<array.length;i++)
		{
			if(max<array[i])
			{
				secondMax = max;
				max=array[i];
			}
			else if(secondMax<array[i])
			{
				secondMax=array[i];
			}
		}
		
		System.out.println("Maximum value: " + max);
		System.out.println("Second maximum value: " + secondMax);
				
	}
}

Output:

Enter array length
4
Enter array values
199
699
100
800
Display array values
199
699
100
800
Maximum value: 800
Second maximum value: 699


Find Minimum, Second minimum value:

import java.util.Scanner;

public class Demo1 {
	
	Scanner sc = new Scanner(System.in);
	static
	{
		System.out.println("Enter array length");
	}
	int len = sc.nextInt();
	int array[] = new int[len];
	int i;
	
	public static void main(String[] args)
	{
		Demo1 demo = new Demo1();
		demo.assignValue();
		demo.displayValue();
		demo.minValue();
	}
	
	public void assignValue()
	{
		System.out.println("Enter array values");
		for(i=0;i<array.length;i++)
		{
		array[i] = sc.nextInt();
		}
	}
	
	public void displayValue()
	{
		System.out.println("Display array values");
		for(i=0;i<array.length;i++)
		{
		System.out.println(array[i]);
		}
	}
	
	public void minValue()
	{
		int min = array[0];
		int secondMin = 0;
		for(i=1;i<array.length;i++)
		{
			if(min>array[i])
			{
				secondMin = min;
				min = array[i];
			}
			else if(secondMin>array[i])
			{
				secondMin = array[i];
			}
			
		}		
		System.out.println("Minimum value: " + min);
		System.out.println("Second minimum value: " + secondMin);				
	}
}

Output:

Enter array length
4
Enter array values
500
50
5000
64
Display array values
500
50
5000
64
Minimum value: 50
Second minimum value: 64


One thought on “ARRAYS BASICS

Leave a comment

Design a site like this with WordPress.com
Get started