Click here to Skip to main content
15,886,724 members

How do I add the resulting arrayList from each button press as a separate element in another arrrayList

Derek Southard asked:

Open original thread
Each time the button is pressed, the result is saved as an arrayList to another arrayList. Problem is it's appending it to the previous element. I need to be able to add the results of each press as a separate element. For example: first press: [5, 3, 5, 2, 6, 5] second press would display: [5, 3, 5, 2, 6, 5][2, 1, 4, 1, 4, 1] This way I can loop through and get each array result separately. How do I do this?

Java
public class mainClass{
        public static void main(String[] args){
JFrame frame1 = new JFrame("testButton");
frame1.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
buttonExample b1 = new buttonExample();
frame1.getContentPane().add(b1);
frame1.pack();
frame1.setVisible(true);
            }

    }

    public class Example {
            private int rand1;
            private ArrayList<ArrayList> count;
            private ArrayList<Integer> count2;
            private Random rnd;
            private int counter1;
            private ArrayList<ArrayList>count3;
        public Example(){
            count = new ArrayList<ArrayList>();
            count2 = new ArrayList<Integer>();
            rnd = new Random();
            count3 = new ArrayList<ArrayList>();
}
        private void addCount2(){
            for(int x = 0; x<6;x++){
            rand1 = rnd.nextInt(6)+1;
            count2.add(rand1);// count2 == Integers
            }
        }

        public void addCount(){
            addCount2();
            count.add(count2);// count == count3
        }
        public ArrayList<ArrayList> displayCount(){
        return count;

        }
}

public class buttonExample extends JPanel {
private JButton button1;
private Example example1;
public buttonExample(){
button1 = new JButton("Submit");
add(button1);
button1.addActionListener(new ButtonListener());
example1 = new Example();
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
    example1.addCount();
    System.out.println(example1.displayCount().get(0));;
    }
}
}
Tags: Java

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900