Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday 8 May 2017

Array Programs In Java

Here we will learn about some most important java array programs. Which is mostly asked in interviews. In the previous chapter, we learned some basic programs in java and String programs in java. Now we will array programs in java programming.


Array Programs In Java



1) Single Dimensional Array In Java

This is very simple program in java array. This is starting point of java array.

class Simple
{
public static void main(String args[])
{
int a[] = new int[5];//declaration and instantiation of array
a[0] = 1;//initialization of an array
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;

//print array
for(int i = 0; i<a.length; i++)
{
System.out.println(a[i]);
}
}


output : 1
              2
              3
              4
              5


2) Multidimensional Array In Java

This is a multidimensional program in java array.

class SimplePrograms
{
public static void main(String args[])
{

//declaration, instantiation, initialization of array
int a[][] = { {1,2,3}, {4,5,6}, {7,8,9}};

//print 2d array
for(int i = 0; i<3; i++)
{
for(int j = 0; j<3; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}

output: 1 2 3
             4 5 6
             7 8 9


3) Sum of Array Elements 

In this program, we are going to add all the elements of an array.

class SumOfArrayElements
{
public static void main(String args[])
{
int sum = 0;
int e[] = {100, 500, 600, 100};
//for each loop
for(int elements : e)
{
sum = sum+elements;
}
System.out.println("array elements sum "+sum);
}
}

output : array elements sum 1300



4) Find Minimum and Maximum Value In Array Java

In this array program we will find the largest and smallest number in an array.

class MinAndMaxInArray
{
public static void main(String args[])
{
int n[] = {1,2,5,10,33,47,4,21,81,37,91,102};

//assign first element of an array to largest and smallest 
int smallest = n[0];
int largest = n[0];
for(int i = 1; i<n.length; i++)
{
if(n[i]>largest)
largest = n[i];
else if(n[i]<smallest)
smallest = n[i];
}

System.out.println(" Largest Number is "+largest);
System.out.println(" Smallest Number is "+smallest);

}
}

output : Largest Number is 102
              Smallest Number is 1


5) Find Duplicates In Array

There are many methods we can use for finding duplicates elements in an java array such as Brute force, HashSet etc. But in this program we we will find the duplicates in an array by using HashSet concept of collection.

import java.util.*;
class DuplicatesElementsInArray
{
public static void main(String args[])
{
//taking String array 
String d[] = {"eye", "leg", "eye", "ear", "hair"};
HashSet<String> hs = new HashSet<String>();
for(String elements : d)
{
if(!hs.add(elements))
{
System.out.println(elements);
}
}
}
}

output : eye


6) Find Common Elements Between Two Arrays In Java

In this java array program we display the common elements between two arrays. Lets take a look below example

import java.util.*;
class CommonElements
{
public static void main(String args[])
{
String s[] = {"red", "yellow", "brown", "orange"};
String s2[] = {"pink", "black", "orange", "red"};
HashSet<String> hs = new HashSet<String>();

for(int i = 0; i<s.length; i++)
{
for(int j = 0; j<s2.length; j++)
{
if(s[i].equals(s2[j]))
{
hs.add(s[i]);
}
}
}
System.out.println("common elements is " + hs);
}
}

Share:

4 comments:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate