Home » Java programming language

Basic Array Operations in Java using Eclipse

In this article, we are going to implement some of the basic array operations in java using eclipse.
Submitted by Raunak Goswami, on August 19, 2018

Hello, guys today we would be performing basic array operations in java in eclipse IDE order to understand the use and importance of array in java the basic array operations are mentioned below:

  1. Creating an array.
  2. Entering values into the array.
  3. Finding the maximum value from the array.
  4. Finding the minimum value from the array.
  5. Searching a particular value in the array using linear search.
  6. Sorting the array using bubble sorting.

Below is the self-explanatory java code is written in eclipse IDE to help you understand the basic operations of arrays in java.

import java.util.*;

class Codechef 
{
	public static void main(String[] args)
	{
		int c=0,k;
		
		//creating an object of scanner class for taking input from the user
		Scanner Sc= new Scanner(System.in);

		System.out.println("Enter the number of elements you want in your array");

		//taking the length of the array from the user using nextInt() method
		int len=Sc.nextInt();

		//creating an array from the user defined length
		int arr[]=new int[len];
		System.out.println("Enter the elements in the array");

		//taking input from the user using for loop
		for(int i=0;i<len;i++)
		{
			//nextInt() method allows the user to enter the values inside the array
			arr[i]=Sc.nextInt();
		}

		// temp variable is used to perform sorting of the variables in the bubble sorting
		int temp=0;

		//using nested loops for bubble sort to sort elements in ascending order
		for(int i=0;i<len;i++)
		{
			for(int j=i+1;j<len;j++)
			{
				if(arr[i]>arr[j])
				{temp=arr[j];
				arr[j]=arr[i];
				arr[i]=temp;}
			}
		}

		System.out.println("The largest element in the array is ");
		//since the elements are sorted in ascending order 
		//the last element is the largest element
		System.out.println(arr[len-1]);

		System.out.println("The smallest element in the array is");

		//this will print the smallest element
		System.out.println(arr[0]);


		System.out.println("Enter the element you want to search");
		//taking the input from the user to perform linear search
		int g=Sc.nextInt();

		//for loop to check whether the given element is present in the array or not
		for(int i=0;i<len;i++)
		{
			if(g==arr[i])
			{
				//to show the actual indexing since the indexing of array starts from 0 
				k=i+1;
				System.out.println("Element found at "+ k+"th " +"position");		
				c=1;
			}
		}//incrementing the value of c variable in case the element is found

		if(c!=1)// if the element is not found 
			System.out.println("Element not found");

		System.out.println("elements in the sorted order");

		// for loop to print the list of elements 
		//that we have sorted using bubble sort
		for(int i=0;i<len;i++)
		{
			//print statement to print the elements of the array
			System.out.println(arr[i]);
		}
	}
}

Output

    Enter the number of elements you want in your array
    6

    Enter the elements in the array
    6
    1
    8
    7
    6
    4
    5

    The largest element in the array is 
    8
    The smallest element in the array is
    1

    Enter the element you want to search
    1

    Element found at 1th position

    elements in the sorted order
    1
    4
    5
    6
    7
    8

Try experimenting with the code written above and make your own array related programs in Java, hope you like this article. Have a great day ahead and keep learning.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.