Java >> Tutoriel Java >  >> Java

Programme Java pour diviser un tableau à partir d'une position spécifiée

Dans ce didacticiel, nous allons apprendre à diviser un tableau à partir d'une position spécifiée. Cela signifie que maintenant le tableau se divisera en deux tableaux distincts. Mais avant d'aller plus loin, si vous n'êtes pas familier avec les concepts du tableau, alors consultez l'article Tableaux en Java.

Saisie : 8 7 9 5 4 3 1 6 0 9 8 3 4

Sortie : Poste =5

Tableau 1 :8 7 9 5 4

Tableau 2 :3 1 6 0 9 8 3 4

Programme 1 :diviser un tableau à partir d'une position spécifiée

Dans cette méthode, nous verrons comment diviser un tableau à partir d'une position spécifiée à l'aide de boucles.

Algorithme

  1. Commencer
  2. Déclarez la taille du tableau.
  3. Demandez à l'utilisateur d'initialiser la taille du tableau.
  4. Déclarez le tableau.
  5. Demandez à l'utilisateur d'initialiser le tableau.
  6. Saisissez la position à partir de laquelle vous souhaitez diviser le tableau.
  7. Déclarez deux autres tableaux pour stocker les tableaux divisés.
  8. Copiez les éléments à la position spécifiée dans un tableau.
  9. Copiez la moitié suivante des éléments dans un autre tableau.
  10. Afficher le tableau.
  11. Arrêtez.

Le programme ci-dessous montre comment diviser un tableau à partir d'une position spécifiée à l'aide de boucles.

/*Java program to split an array from a specified position*/
import java.util.*;  

public class Main  
{  
   public static void main(String args[])   
   {  
       Scanner sc=new Scanner(System.in);

      int n;    //Declare array size
      System.out.println("Enter the total number of elements ");
      n=sc.nextInt();     //Initialize array size

      int arr[]=new int[n];   //Declare array
      System.out.println("Enter the elements of the array ");
      for(int i=0; i<n ;i++)     //Initialize array
      {
          arr[i]=sc.nextInt();
      }
      
      System.out.println("Enter the position of the array : ");
      int pos=sc.nextInt();    //Enter the position from where you want to split the array
      
      int brr[]=new int[pos];   //Array to store the first half
      int z=n-pos;
      int crr[]=new int[z];    //Array to store the second half of the array
      
      //Display the elements of the original array 
      System.out.println("Original Array Elements are ");
      for(int i=0;i<n;i++)
       {
           System.out.print(arr[i]+ " ");
       }
       System.out.println("");

      //Copy the first half elements
      for(int i=0;i<pos;i++)
      {
          brr[i]=arr[i];
      }
      //Print the first half elements
      System.out.println("The first array elements are : ");
       for(int i=0;i<pos;i++)
       {
           System.out.print(brr[i]+ " ");
       }
       System.out.println("");
       
       //Copy the second half elements
       int k=0;
       for(int i=pos;i<n;i++)
       {
           crr[k]=arr[i];
           k++;
       }
       //Print the second half elements
       System.out.println("The second array elements are : ");
       for(int t=0;t<z;t++)
       {
           System.out.print(crr[t]+ " ");
       }
       
   }  
}  


Entrez le nombre total d'éléments 10
Entrez les éléments du tableau 2 3 4 1 5 6 7 2 8 9
Entrez la position du tableau :4
Original Array Elements sont
2 3 4 1 5 6 7 2 8 9
Les éléments du premier tableau sont :
2 3 4 1
Les éléments du second tableau sont :
5 6 7 2 8 9

Programme 2 :diviser un tableau à partir d'une position spécifiée

Dans cette méthode, nous verrons comment diviser un tableau à partir d'une position spécifiée en utilisant Arrays.copyofRange() méthode.

Algorithme

  1. Commencer
  2. Déclarez la taille du tableau.
  3. Demandez à l'utilisateur d'initialiser la taille du tableau.
  4. Déclarez le tableau.
  5. Demandez à l'utilisateur d'initialiser le tableau.
  6. Saisissez la position à partir de laquelle vous souhaitez diviser le tableau.
  7. Déclarez deux autres tableaux pour stocker les tableaux divisés.
  8. Copiez les éléments à la position spécifiée dans un tableau en utilisant Arrays.copyofRange() méthode.
  9. Copiez la prochaine moitié des éléments dans un autre tableau en utilisant Arrays.copyofRange() méthode.
  10. Afficher le tableau.
  11. Arrêtez.

Le programme ci-dessous montre comment diviser un tableau à partir d'une position spécifiée en utilisant Arrays.copyofRange() méthode.

/*Java program to split an array from a specified position*/
import java.util.*;  
import java.util.Arrays; 

public class Main  
{  
   public static void main(String args[])   
   {  
       Scanner sc=new Scanner(System.in);

      int n;    //Declare array size
      System.out.println("Enter the total number of elements ");
      n=sc.nextInt();     //Initialize array size

      int arr[]=new int[n];   //Declare array
      System.out.println("Enter the elements of the array ");
      for(int i=0; i<n ;i++)     //Initialize array
      {
          arr[i]=sc.nextInt();
      }
      
      System.out.println("Enter the position of the array : ");
      int pos=sc.nextInt();    //Enter the position from where you want to split the array
      
      int brr[]=new int[pos];   //Array to store the first half
      int z=n-pos;
      int crr[]=new int[z];    //Array to store the second half of the array
      
      //Display the elements of the original array 
      System.out.println("Original Array Elements are ");
      for(int i=0;i<n;i++)
       {
           System.out.print(arr[i]+ " ");
       }
       System.out.println("");

      //Copy the first half elements
      brr=Arrays.copyOfRange(arr, 0, pos); 
      
      //Print the first half elements
      System.out.println("The first array elements are : ");
       for(int i=0;i<pos;i++)
       {
           System.out.print(brr[i]+ " ");
       }
       System.out.println("");
       
       //Copy the second half elements
       crr=Arrays.copyOfRange(arr, pos, n); 
       
       //Print the second half elements
       System.out.println("The second array elements are : ");
       for(int t=0;t<z;t++)
       {
           System.out.print(crr[t]+ " ");
       }
       
   }  
}  


Entrez le nombre total d'éléments 10
Entrez les éléments du tableau 4 5 8 3 1 4 6 7 3 2
Entrez la position du tableau :6
Original Array Elements sont
4 5 8 3 1 4 6 7 3 2
Les premiers éléments du tableau sont :
4 5 8 3 1 4
Les deuxièmes éléments du tableau sont :
6 7 3 2


Balise Java