将列表添加到另一个列表

时间:2016-10-04 12:04:42

标签: java list arraylist

首先,我需要将用户提供的数字添加到名为 numberlist 的列表中。然后最后将其添加到名为 listCollection 的列表列表中。之后我需要显示listCollection的输出 例如,首先让用户在列表中添加2,3,4,然后点击添加列表集,然后再次输入数字5,6,7,8并点击添加到列表集。我想要显示的输出是

Collection1 : 2,3,4
Collection2 : 3,4,5

我只想说 listCollection 的索引0中的2,3,4和 listCollection的索引1中的5,6,7
这是我的代码:

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

import javax.swing.JTextField;

/**
 *
 * @author sudip
 */
public class JavaApplication1 extends JFrame implements ActionListener {
    private JTextField txtNotation;
    private JButton btAddlist, btAddNumber, btOutput;
    private List<List<Integer>> listCollection;
    private History history;

    public JavaApplication1() {
        initComponents();
        listCollection = new ArrayList<List<Integer>>();
    }

    public void initComponents() {
        setSize(400, 500);
        setLayout(new FlowLayout(3, 3, 3));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        txtNotation = new JTextField("2");
        txtNotation.setColumns(20);
        txtNotation.setSize(20, 40);
        btAddNumber = new JButton("Add Number");
        btAddlist = new JButton("Add List to Collection");
        btOutput = new JButton("Get");
        getContentPane().add(txtNotation);
        getContentPane().add(btAddNumber);
        getContentPane().add(btAddlist);
        getContentPane().add(btOutput);
        btAddlist.addActionListener(this);
        btAddNumber.addActionListener(this);
        btOutput.addActionListener(this);
    }

    public static void main(String[] args) {
        new JavaApplication1().setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton source = (JButton) e.getSource();
        List<Integer> numberlist = new ArrayList<>();
        int a = Integer.parseInt(txtNotation.getText());

        if (source.equals(btAddNumber)) {
            System.out.println("Number was added to list");
            numberlist.add(a);
        } else if (source.equals(btOutput)) {
            displayOutput();
        } else if (source.equals(btAddlist)) {
            System.out.println("Number of list was added to ListCollection");
            numberlist.clear();
            listCollection.add(numberlist);
        }

    }

    public void displayOutput() {
        for (int i = 0; i < listCollection.size(); i++) {
            System.out.println("Collection: " + i+1);
            for (int j = 0; j < listCollection.get(i).size(); j++) {
                System.out.print(listCollection.get(i).get(j) + ", ");
            }
        }
    }

}

3 个答案:

答案 0 :(得分:1)

您的问题存在于actionPerformed逻辑中,每次重新验证新的numberList。当事件的来源是btAddList时,您的列表将始终为空,因为它刚刚被重新创建。 btAddNumber按钮添加的数字永远不会保留,修改后的numberList会在退出actionPerfomed方法的范围后丢失。

如评论中所述,您应该将GUI与逻辑分开,这有助于避免这种错误。

答案 1 :(得分:0)

稍微不清楚你在问什么,但我可以看到两种可能性:

  1. 在索引collection1上创建一个列表0collection2 1的列表。
  2. 克里特岛列表中包含collection1collection2的所有元素。
  3. 第一种方法:

    List<List<Integer>> listCollection = new ArrayList<>();
    listCollection.add(collection1);
    listCollection.add(collection2);
    

    List<List<Integer>> listCollection = new ArrayList<>();
    listCollection.add(null);
    listCollection.add(collection1);
    listCollection.add(collection2);
    

    如果你想从索引1开始非常精通 - 只是说。

    第二种方法:

    List<Integer> listCollection = new ArrayList<>();
    listCollection.addAll(collection1);
    listCollection.addAll(collection2);
    

答案 2 :(得分:0)

 @Override
    public void actionPerformed(ActionEvent e) {
        JButton source = (JButton) e.getSource();
        //List<Integer> numberlist = new ArrayList<Integer>(); //you clear your list here
        int a = Integer.parseInt(txtNotation.getText());

        if (source.equals(btAddNumber)) {
            System.out.println("Number was added to list");
            numberlist.add(a);
        } else if (source.equals(btOutput)) {
            displayOutput();
        } else if (source.equals(btAddlist)) {
            System.out.println("Number of list was added to ListCollection");
            //numberlist.clear();   //you then added empty list to your collection (cause add doesn't copy it, it adds a reference to it)
            listCollection.add(numberlist);
            numberlist = new ArrayList<Integer>(); //creates new list instead of clearing old one
        }

    }

结果:

Number was added to list
Number was added to list
Number was added to list
Number of list was added to ListCollection
Number was added to list
Number was added to list
Number was added to list
Number was added to list
Number of list was added to ListCollection
Collection: 01 
2, 3, 4, 
Collection: 11 
5, 6, 7, 8,