2016-10-22 247 views
0

這是我正在處理的一項小工作。我是一名初學Java程序員,在這個小程序中遇到了一個問題。出於某種原因,如果數組包含10個元素,則刪除方法不起作用。我很樂意就解決這個問題提供一些建議/幫助。從「完整」陣列中刪除元素

當數組包含10個元素,並且我嘗試刪除一個元素時,textArea中元素的排列方式並非如此。一切都應該改變1,包含9個元素,最後一個元素爲0.當一個元素被刪除時,所有元素都應該調整自己並向左移動。

輸出的默認外觀是:[0,0,0,0,0,0,0,0,0,0]

如果我輸入數字1 - 10,輸出應該是這樣的: [1,2,3,4,5,6,7,8,9,10]

如果用戶試圖刪除最後一個元件,但是將其設置爲0: [1,2,3,4,5,6,7,8,9,0]

H但是,如果用戶試圖刪除另一個元素,比如5,那麼程序會在控制檯中拋出一個超出界限的異常。如果它們除去5,輸出應該是:[1,2,3,4,6,7,8,9,10,0]

更多上下文中,這裏的分配細節:

Create a complete program that has the ability to store and display integer values in an array. The maximum number of values that your program should be able to handle is 10.

  • Add additional elements to the array using screen input (textbox and button).
  • Remove array elements based on screen input (textbox and button).
  • List all of the elements in the array and compute the sum of all the elements.
  • List the even elements in the array and compute the sum of the even elements.
  • List the odd elements in the array and compute the sum of the odd elements.
import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.util.Arrays; 

    public class SumElement implements ActionListener { 

    public static JButton add, remove, sumAll, sumEven, sumOdd; 
    public static JTextField inputField; 
    public static JTextArea textArea; 
    public static int counter = 0; 
    public static int[] numbers = new int[10]; 
    public static JLabel titleText; 

    public static void main(String[] args) { 
     System.out.println("Counter: " + counter); 

     // Frame 
     JFrame frame = new JFrame("Integer Sums"); 
     frame.setSize(178, 240); 
     frame.setResizable(false); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // Container panel 
     JPanel container = new JPanel(); 
     container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS)); 
     frame.setContentPane(container); 

     // Title panel 
     JPanel titlePane = new JPanel(); 
     titleText = new JLabel("Integer Sums"); 

     // Content panel 
     JPanel content = new JPanel(); 
     content.setPreferredSize(new Dimension(300, 180)); 
     content.setLayout(null); 

     // Buttons 
     add = new JButton("Add"); 
     add.setBounds(4, 50, 80, 20); 
     add.addActionListener(new SumElement()); 

     remove = new JButton("Remove"); 
     remove.setBounds(88, 50, 80, 20); 
     remove.addActionListener(new SumElement()); 

     sumAll = new JButton("Sum All"); 
     sumAll.setBounds(4, 74, 164, 20); 
     sumAll.addActionListener(new SumElement()); 

     sumEven = new JButton("Sum Even"); 
     sumEven.setBounds(4, 98, 164, 20); 
     sumEven.addActionListener(new SumElement()); 

     sumOdd = new JButton("Sum Odd"); 
     sumOdd.setBounds(4, 122, 164, 20); 
     sumOdd.addActionListener(new SumElement()); 

     // Text area, and text field 
     inputField = new JTextField(); 
     inputField.setBounds(4, 21, 164, 25); 

     JLabel inputLabel = new JLabel("Input integer value below:"); 
     inputLabel.setBounds(12, 6, 164, 13); 

     textArea = new JTextArea(); 
     JScrollPane scrollPane = new JScrollPane(textArea); 
     scrollPane.setBounds(4, 144, 165, 36); 

     // Adding everything 
     container.add(titlePane); 
     titlePane.add(titleText); 
     container.add(content); 
     content.add(inputLabel); 
     content.add(inputField); 
     content.add(scrollPane); 
     content.add(add); 
     content.add(remove); 
     content.add(sumAll); 
     content.add(sumEven); 
     content.add(sumOdd); 

     // Extras 
     frame.toFront(); 
     frame.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent event) { 
     if (event.getActionCommand().equals("Add")) { 
      if (counter == 10) { 
       titleText.setText("Error: too many values"); 
      } else { 
       numbers[counter] = Integer.parseInt(inputField.getText()); 
       textArea.setText(Arrays.toString(numbers)); 
       counter++; 
      } 
      System.out.println("Counter: " + counter); 
     } else if (event.getActionCommand().equals("Remove")) { 
      for (int i = 0; i < counter; i++) { 
       if (Integer.parseInt(inputField.getText()) == numbers[i]) { 
        for (int x = i; x < counter - 1; x++) { 
         numbers[x] = numbers[x + 1]; 
        } 
        textArea.setText(Arrays.toString(numbers)); 
        counter--; 
       } else { 
        titleText.setText("Error: value doesn't exist"); 
       } 
      } 
      System.out.println("Counter: " + counter); 
     } else if (event.getActionCommand().equals("Sum All")) { 
      int sum = 0; 
      for (int i = 0; i <= counter; i++) { 
       sum += numbers[i]; 
      } 
      titleText.setText("Sum is " + sum); 
     } else if (event.getActionCommand().equals("Sum Even")) { 
      int sum = 0; 
      for (int i = 0; i <= counter; i++) { 
       if (numbers[i] % 2 == 0) { 
        sum += numbers[i]; 
       } 
      } 
      titleText.setText("Sum of even values is: " + sum); 
     } else if (event.getActionCommand().equals("Sum Odd")) { 
      int sum = 0; 
      for (int i = 0; i <= counter; i++) { 
       if (numbers[i] % 2 != 0) { 
        sum += numbers[i]; 
       } 
      } 
      titleText.setText("Sum of odd values is: " + sum); 
     } 
    } 
} 

下面是代碼修改後的actionPerformed部分。現在刪除最後一個數組值。但是,如果數組已滿,並且用戶試圖刪除最後一個元素以外的元素,則會引發錯誤。我仍然不確定如何解決這個錯誤。

public void actionPerformed(ActionEvent event) { 
     if (event.getActionCommand().equals("Add")) { 
      if (counter == 10) { 
       titleText.setText("Error: too many values"); 
      } else { 
       numbers[counter] = Integer.parseInt(inputField.getText()); 
       textArea.setText(Arrays.toString(numbers)); 
       counter++; 
      } 
      System.out.println("Counter: " + counter); 
     } else if (event.getActionCommand().equals("Remove")) { 
      if (Integer.parseInt(inputField.getText()) == numbers[9]) { 
       numbers[9] = 0; 
       counter--; 
       textArea.setText(Arrays.toString(numbers)); 
      } else { 
       for (int i = 0; i < counter; i++) { 
        if (Integer.parseInt(inputField.getText()) == numbers[i]) { 
         for (int x = i; x <= counter - 1; x++) { 
          numbers[x] = numbers[x + 1]; 
         } 
         textArea.setText(Arrays.toString(numbers)); 
         counter--; 
        } 
       } 
      } 
      System.out.println("Counter: " + counter); 
     } else if (event.getActionCommand().equals("Sum All")) { 
      int sum = 0; 
      for (int i = 0; i <= counter; i++) { 
       sum += numbers[i]; 
      } 
      titleText.setText("Sum is " + sum); 
     } else if (event.getActionCommand().equals("Sum Even")) { 
      int sum = 0; 
      for (int i = 0; i <= counter; i++) { 
       if (numbers[i] % 2 == 0) { 
        sum += numbers[i]; 
       } 
      } 
      titleText.setText("Sum of even values is: " + sum); 
     } else if (event.getActionCommand().equals("Sum Odd")) { 
      int sum = 0; 
      for (int i = 0; i <= counter; i++) { 
       if (numbers[i] % 2 != 0) { 
        sum += numbers[i]; 
       } 
      } 
      titleText.setText("Sum of odd values is: " + sum); 
     } 
    } 
} 
+0

我認爲反 - 1中爲remove元素的循環是問題。它應該是x <=計數器。將下一個值移動到當前值應該是條件I.e.如果第10個元素被移除,只需將第10個元素賦值爲0,而不是將下一個元素移至當前。 – notionquest

+2

請更具體地說明'它不工作的意義'。你可以做的最好的事情是提供一個'1:輸入集合'(你的數組中有什麼,例如10,9,...,1),'2:你對它做了什麼'(例如「我試圖去除編號3「),'3:預期結果'(例如,我期望數組是......),'4:實際結果' – Ivan

+0

編輯問題,具體細節。 – jxshu

回答

1

如果存在,下面的代碼應該正確地刪除整數值。但是,它不處理數組中存在多個相同整數值的情況(即數組有1,1,1,1,1,1,1,0,0,0並嘗試刪除1)。它不會刪除全部1,並在文本框中顯示全0。

我不知道的要求是否有數組中的唯一值。如果是這種情況,應該修改add函數來處理場景。

else if (event.getActionCommand().equals("Remove")) { 
      boolean removed = false; 
      titleText.setText("Integer Sums"); 
      for (int i = 0; i < counter; i++) { 
       if (Integer.parseInt(inputField.getText()) == numbers[i]) { 
        for (int x = i; x < counter; x++) { 
         if (x == 9) { 
          numbers[x] = 0; 
         } else { 
          numbers[x] = numbers[x + 1]; 
          numbers[x + 1] =0; 
         } 
        } 
        textArea.setText(Arrays.toString(numbers)); 
        counter--; 
        removed = true; 
       }    
      } 
      if (!removed) { 
       titleText.setText("Error: value doesn't exist"); 
      } 
      System.out.println("Counter: " + counter); 
     } 
+0

非常感謝,這個作品很棒。 – jxshu

0

問題應該在這個代碼留置權:

for (int x = i; x <= counter - 1; x++) { 
    numbers[x] = numbers[x + 1]; 
} 

然後,陣列包含10種元素的變量計數器是10 在這種情況下,迭代將努力對付-1這是9.在下一行中,您嘗試獲得10 [(x = 9)+1]的「numbers [x + 1]」,因此出現界限。 如果將它更改爲計數器-2而不是-1,它應該可以工作,因爲atm可以從下一個字段獲取元素以填充應該仍然可以工作的位置。

+1

當我嘗試時,該程序不再有效。如果輸出結果爲[1,2,3],並且我移除了2,則它變爲[1,3,3]而不是它應該是的:[1,3,0]。 – jxshu

+0

@jzbakos這些示例數字是所有人都會發現真正有用的回答你的問題 – Ivan

+0

啊,對不起。現在檢查帖子。 – jxshu