简体   繁体   中英

java: Array not Re-initializing but it's duplicating according to the user's array limit

this is the code I've been working on, the problem is it won't reinitialize to what I enter at menu#2 it just duplicate the array limit. if you insert a value in the array it won't remove the "0" value of the array instead it will leave the 0 alone and duplicate the array into a new one which adds a new set of lines that contains the target result. thanks for the help.

`import java.util.Scanner;
 import java.util.Arrays;

 public class Testcode
 {
  public static void main(String[] args){
  process nu = new process();
  nu.m();
  }
 }
   class process{
   Scanner in = new Scanner(System.in); 
   int choice1 =0;
   int limit1;
   
   public void m(){
    System.out.println("                   ARRAY OPERATION");
    System.out.println("                        menu");
    System.out.println("                 [1] Create Array");
    System.out.println("                 [2] Insert Element");
    System.out.println("                 [3] Search");
    System.out.println("                 [4] Display");
    System.out.println("                 [5] Delete");
    System.out.println("                 [0] Stop");
    System.out.print("                 Enter choice:");
     e();
 }
    
  public void e(){
    
     //int check = 0;
    
     int array[] = new int[limit1];
    
     choice1 = in.nextInt();
     cls();
  
      if (choice1 == 1)
      {
       
        System.out.println("Create Array");
        System.out.println("Enter the limit of your array: ");
        limit1 = in.nextInt();
        
        
            if(limit1 <5){
            System.out.println("Error: Minimum limit exceeded"); 
            System.out.println("Going back to main menu");
            m(); //loop to main menu
        }
        if(limit1 >20){
            System.out.println("Error: Maximum limit exceeded");
            System.out.println("Going back to main menu");
            m(); //loop to main menu
        }
        System.out.println("An array with a limit of " + limit1 + " has been created");
        m();
   }
    
    
   if(choice1 == 2)
    {
      
      System.out.println("Enter the " + limit1 + " numbers now.");
       
      for (int i = 0 ; i < array.length; i++ ) {
       array[i] = in.nextInt();
      }
      System.out.println("You have entered the "+ limit1 + " numbers");
      m();
      
   } 
   
   if(choice1 == 4){
      
    System.out.println(Arrays.toString(array));

   }
}
 

     private void cls(){
    System.out.print('\u000C');
   }
  }    
  `

Output after inserting a value inside the array

Let's reiterate of how your program is working

  1. Program starts for the first time, new instance of process class is created.
  2. The method m() is called, and menu is displayed.
  3. From m() another method e() is called, which creates a new array with limit 0 (because default value of limit1 is 0). The value of array = [] - because of no elements.
  4. You input a choice "1", you enter the limit now (lets say limit1 = 6 at the moment)
  5. The original array you defined above, remains unaffected. You just changed the limit1 variable, not the array itself.
  6. Now, the flow goes back to the m() method and MENU is displayed again.
  7. You input the choice as 2 in order to enter the array elements.
  8. In the method e() a new array is created, but with the previous value of the limit1 ie 6. Therefore, int[] array = new int[6] is created and then you input your elements.

The problem with the code is that you are re-creating the array everytime a user inputs a choice. What you need to do is to declare the array variable in the class process For example:

class process{
   Scanner in = new Scanner(System.in); 
   int choice1 =0;
   int limit1;
   int[] array;

Next, in the method e() remove the creation of array by removing the line

int array[] = new int[limit1];

In choice 1, where you want to create the array, add the following line, when you input the limit.

limit1 = in.nextInt();
array = new int[limit1];

At this point, NOW your array is actually created only when a user presses 1. This should fix your program.

我认为你应该在顶部初始化 limit1,这可能是问题所在

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM