[英]Java Recursive Method ArrayList is not adding all child elements to the final ArrayList
[英]Recursive method for multiplying arraylist elements
我希望我的措辭不會太混亂。 我正在做一個關於遞歸的學校項目。 我構建了一個 GUI 以允許用戶在程序中輸入數字。 我想將用戶輸入到 textField 中的數字存儲在數組列表中,以便我可以使用遞歸方法將所有數字自己相乘。 我能夠創建數組列表並存儲數字,但我似乎無法弄清楚如何遞歸地將 ArrayList 中的元素相乘。 我以為我可以做這樣的事情:
numbers.get(numbers.size()) * (numbers.size() - 1)
以上不起作用,因為索引總是超出范圍。 也不確定我是否了解這里發生的事情。
我有一個單獨的驅動程序 class 調用 GUI class 來運行程序。 見下文。
public class Gui {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
//Array list to store user input.
public static List<Integer> numbers = new ArrayList<>();
public static int num;
//recursive Method
public static int calculate(){
return(0);
}
//Gui Program
public static void addComponents(Container pane){
if(RIGHT_TO_LEFT){
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JLabel label;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if(shouldFill){
c.fill = GridBagConstraints.HORIZONTAL;
}
//Instruction Label
label = new JLabel("Welcome to recursion! Enter 5 numbers below.");
if (shouldWeightX){
c.weightx = 0.5;
}
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.insets = new Insets(5, 10, 5, 10);
pane.add(label, c);
JLabel label1 = new JLabel("Enter Numbers:");
c.gridx = 0;
c.gridy = 1;
pane.add(label1, c);
JTextField tf = new JTextField();
//Accepts only numbers 1 - 9. Zero defeats the purpose of the program.
tf.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent ke) {
tf.getText();
if(ke.getKeyChar()>='1' && ke.getKeyChar()<='9' || ke.getKeyChar()== KeyEvent.VK_BACK_SPACE){
tf.setEditable(true);
}else{
tf.setEditable(false);
label.setText("You must enter numeric digits 1-9");
}
}
});
c.gridx = 1;
c.gridy = 1;
c.insets = new Insets(0, 105, 0,10);
pane.add(tf, c);
JLabel outLabel = new JLabel(" ");
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 2;
c.insets = new Insets(0,10,10,0);
pane.add(outLabel, c);
JButton b = new JButton("Submit");
b.addActionListener(e -> {
try {
num = Integer.parseInt(tf.getText());
numbers.add(num);
outLabel.setText(String.valueOf(numbers));
tf.setText("");
}catch (Exception x){
outLabel.setText("Please make sure you have entered a number!");
}
});
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 2;
c.insets = new Insets(10,25, 10,25);
pane.add(b, c);
JButton b2 = new JButton("Multiply!");
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 2;
c.insets = new Insets(0,25, 10,25);
pane.add(b2, c);
}
public static void createGui(){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Recursive Multiplication");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponents(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
}
首先,您必須為乘法按鈕創建邏輯,例如:
JButton b2 = new JButton("Multiply!");
b2.addActionListener(e -> {
try {
num = Integer.parseInt(tf.getText());
int result = multiple_recursive(numbers, 0);
// choose label to set the value.
}catch (Exception x){
// solve exception
}
});
然后定義了遞歸乘法的方法:
public static int multiple_recursive(List<Integer> numbers, int count){
if(numbers.size() == count){
return 1;
}
return numbers.get(count) * multiple_recursive(numbers, count + 1);
}
所以這個想法是你傳遞list
和一個count
變量。 這個count
將告訴當前 position 的數字列表。 對於每個遞歸調用,您從列表numbers.get(count)
中獲取當前元素並再次遞歸調用,但將當前count
加一,即multiple_recursive(numbers, count + 1);
. 一旦當前count
與列表大小相同,遞歸調用就應該停止,我們返回 1,因為該值的乘法標識屬性。
對於具有元素{5, 2, 10}
的列表。 迭代將如下所示:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.