Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
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?
 
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));;
    }
}
}
Posted 9-Dec-12 6:07am
Edited 9-Dec-12 19:48pm
TorstenH.33.3K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Problem is in function addCount2(). You are extending the List of count2, instead you should init it new to delete the "old values":
 
 private void addCount2(){
      count2 = new ArrayList<integer>(); // set up count2 clean and new and EMPTY!
      for(int x = 0; x<6;x++){
      int rand1 = rnd.nextInt(6)+1;
      count2.add(rand1);// count2 == Integers
   }
 }
</integer>
 
You should also limit the scope of some variables some are only used in single functions and should therefor only exist in there.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,745
1 OriginalGriff 6,696
2 CPallini 5,315
3 George Jonsson 3,599
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100