Jese Rodeiz
Jese Rodeiz

Reputation: 17

Add lists to another list

First i need to add the numbers given by user to a list named numberlist. And then finally add it to the list of lists named listCollection. After that i need to display the output from the listCollection For example lets say at first users add numbers 2,3,4 to the list and then clicks Add list Collection, and then again inputs numbers 5,6,7,8 and clicks Add to list Collection. The ouput i want do display is

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

lets just say i want 2,3,4 in index 0 of listCollection and 5,6,7 in Index 1 of listCollection.
Here's my code:

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) + ", ");
            }
        }
    }

}

Upvotes: 0

Views: 116

Answers (3)

Filipp Voronov
Filipp Voronov

Reputation: 4197

 @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
        }

    }

Result:

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, 

Upvotes: 0

Aaron
Aaron

Reputation: 24802

Your problem resides in your actionPerformed logic, where you reinstantiate a new numberList each time. When the source of the event is the btAddList, your list will always be empty since it just has been recreated. Numbers added by the btAddNumberbutton will never be persisted, the modified numberList being lost after the scope of the actionPerfomed method has been exited.

As said in the comments, you should aim to separate your GUI from your logic, that would have helped avoiding such mistake.

Upvotes: 1

xenteros
xenteros

Reputation: 15842

It's slightly unclear what you're asking, however I can see two possibilities:

  1. Create a list which will have collection1 on index 0 and collection2 on index 1.
  2. Crete a list which will have all the elements of collection1 and collection2.

First approach:

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

or

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

in case you want to start with index 1 very studid - just saying.

The second approach:

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

Upvotes: 0

Related Questions