Java >> Tutoriel Java >  >> Java

Opérations de base sur les tableaux en Java avec Eclipse

Bonjour, les gars, aujourd'hui, nous effectuerions des opérations de tableau de base en Java dans l'IDE Eclipse Afin de comprendre l'utilisation et l'importance des tableaux en Java, les opérations de base sur les tableaux sont mentionnées ci-dessous :

  1. Création d'un tableau.
  2. Saisir des valeurs dans le tableau.
  3. Recherche de la valeur maximale à partir du tableau.
  4. Rechercher la valeur minimale dans le tableau.
  5. Recherche d'une valeur particulière dans le tableau à l'aide de la recherche linéaire.
  6. Trier le tableau à l'aide du tri à bulles.

Ci-dessous, le code Java explicite est écrit dans l'IDE Eclipse pour vous aider à comprendre les opérations de base des tableaux en 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]);
		}
	}
}

Sortie

    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

Essayez d'expérimenter avec le code écrit ci-dessus et créez vos propres programmes liés aux tableaux en Java, j'espère que cet article vous plaira. Passez une excellente journée et continuez à apprendre.


Balise Java