Array basics in java

Array:-

Image result for what is array?

An array is a collection of similar data elements stored at contiguous memory locations. It is the simplest data structure where each data element can be accessed directly by only using its index number.

Reference:–(https://www.mygreatlearning.com/blog/what-is-an-array-learn-more-in-one-read/)

Sample Program:- Print the number where the state placed

package payilagam.institution;

public class Array {

	public static void main(String[] args) 
	{
		int[] array= {10,20,30,40,50};
		System.out.println(array[4]);
	}

}

Output Screenshot:-

Program2:- Print the values in the array braces

package payilagam.institution;

public class Array1 {

	public static void main(String[] args) 
	{   
		int[] array= {10,13,15,18,16};
		for(int i=0;i<array.length;i++)
		{
			System.out.println(array[i]);
		}
	}
}

Output Screenshot:-

Program 3:- Array Divisors

package payilagam.institution;

public class array3 {

	public static void main(String[] args) 
	{   int div=2;
		int[] array= {11,12,13,14,15,16,17};
		for(int i=0;i<array.length;i++)
		{
			if(array[i]%div==0)
			{
		System.out.println("Even number is: "+array[i]);
			}
			else
			{
		System.out.println("Odd number is:  "+array[i]);
			}
		}
	}
}

Output Screenshot:-

Program 4:- To print String and Char

package payilagam.institution;

public class array4 {

	public static void main(String[] args) 
	{
		String[] name= {"hii","hello","welcome"};
		for(int i=0;i<name.length;i++)
		{
			System.out.println(name[i]);
		}
		char[] ch= {'S','U','T','H','A','G','A','R'};
		for(int i=0;i<ch.length;i++)
		{
			System.out.print(ch[i]);
		}
		
	}

}

Output Screenshot:-

Leave a Reply

Create your own website, free on WordPress.com.Create your own website.
Start free