Apr 24

Write a java program to check whether an array contains duplicates or not?

In this tutorial we will write a program to check whether an array contains duplicates.First we will write steps how to solve this then we will write a program.

Steps to check whether array contains duplicates or not

1)  create and initialize input array.

2)  Convert the array to list.

3)  Create an hashset using list. Continue reading

Apr 22

write a java program to check two arrays are equal or not ?

This tutorial explains how to check two arrays are equal or not with help of an example.Arrays.equals() method is used to check two arrays are equal or not .

Steps to write program:

1) Create and intialize two arrays.

2) Compare two arrays by using Arrays.equals() method which takes tw arrays and returns boolean value if they are true else it returns false. Continue reading

Apr 22

How to create and initialize array in java?

Java example to create and initialize array in java

public class ArraysDemo {

	public static void main(String[] args) {

		//Creating an array

		int a[]=new int[5];

		//Adding elements to array by accessing its index

		a[0]=1;
		a[1]=2;
		a[2]=3;
		a[3]=4;
		a[4]=5;
		a[5]=6;

		//Printing the elements in array
		// length variable is used to know the size of the array
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i]);
		}

	}

}