Welcome to Dream.In.Code
Become a Java Expert!

Join 150,053 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,739 people online right now. Registration is fast and FREE... Join Now!




Using an ArrayList<Integer> of counters to keep track of the nu

 
Reply to this topicStart new topic

Using an ArrayList<Integer> of counters to keep track of the nu

C:/Syntax
4 Jun, 2008 - 06:53 AM
Post #1

New D.I.C Head
*

Joined: 12 Apr, 2008
Posts: 23



Thanked: 1 times
My Contributions
I am rolling dice (two die) n number of times. From the n number of rolls, I need to count how many times I got each each occurrence.
The possibilities of two dies rolling is a sum of 2 to 12.
I made an ArrayList of 13 elements.
Now I have to find a way to add each possibility into the ArrayList.
With the ArrayList I will not need 11 if statements.
How do I add each possibility to the ArrayList.
I am using a Dice class I made, everyhing is compiling in the Applet.

My Code:
CODE
import java.util.*;//array list
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

/**
* Class DiceRoll - write a description of the class here
*
* @author (your name)
* @version (6a-4)
*/
public class DiceRoll extends JApplet
                      implements ActionListener
{
private Dice dice1, dice2;
private JLabel numRollLabel, pressEnter, dice1Label, dice2Label;
private JTextField numRollField, field2;
private JPanel rollPanel, countPanel;
private ArrayList <Integer> sumList;
private int sum=0;

    public void init()
    {
        //********north**********************
        rollPanel = new JPanel();
        numRollLabel = new JLabel("Number of Rolls");
        numRollField = new JTextField("", 10);
        numRollField.addActionListener(this);
        pressEnter = new JLabel("Press Enter When Ready");
        pressEnter.setForeground(Color.pink);
        dice1 = new Dice();
        dice2 = new Dice();
        dice1Label = new JLabel(dice1.getPic());
        dice2Label = new JLabel(dice2.getPic());

        rollPanel.setLayout(new FlowLayout());
        rollPanel.add(numRollLabel);
        rollPanel.add(numRollField);
        rollPanel.add(pressEnter);
        rollPanel.add(dice1Label);
        rollPanel.add(dice2Label);
        //*************South*****************
        countPanel = new JPanel();
        field2 = new JTextField("",3);
        
        countPanel.setLayout(new FlowLayout());
        countPanel.add(field2);

        
        Container window = getContentPane();
        window.setLayout(new BorderLayout ());
        window.add(rollPanel, BorderLayout.NORTH);
        window.add(countPanel, BorderLayout.SOUTH);
        
        sumList = new ArrayList<Integer>();
        for(int i=0; i<=13; i++)
        {
            sumList.add(0);
        }
    }
    
    public void actionPerformed( ActionEvent ae)
    {
        if(ae.getSource()==numRollField)
        {
            roll();
        }
    }
    
    public void roll()
    {
        int x, rolls;
        int count2=0;

        rolls = stringToInt(numRollField.getText());
        for(x = 0; x<=rolls; x++)
        {
            dice1.roll();
            dice2.roll();
            dice1Label.setIcon(dice1.getPic());
            dice2Label.setIcon(dice2.getPic());
            sum = dice1.getRoll()+ dice2.getRoll();
        }
        field2.setText(""+sum);



        numRollField.setText("");//last action
    }
    
    private int stringToInt(String strObj)
    {
        return Integer.parseInt(strObj.trim());
    }

}


User is offlineProfile CardPM
+Quote Post

cutegrrl
RE: Using An ArrayList<Integer> Of Counters To Keep Track Of The Nu
4 Jun, 2008 - 08:40 AM
Post #2

D.I.C Head
**

Joined: 12 May, 2008
Posts: 72



Thanked: 7 times
My Contributions
java


import java.util.*;

public class Dice {

private static Random r = new Random();
private static int[] list = new int[11];


public static void main(String[] args) {

roll(10);

for(int i = 0; i < list.length; i++){
System.out.println("Sum " + (i+2) + ": " + list[i]);
}

}


public static void roll(int n){
for (int i = 0; i < n; i++) {
list[((r.nextInt(6) + 1) + (r.nextInt(6) + 1))-2]++;
}
}

}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Using An ArrayList<Integer> Of Counters To Keep Track Of The Nu
4 Jun, 2008 - 09:51 AM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Cutegrrl is right. In your case using an ArrayList is like using a limo to deliver a pizza. ArrayList are used when you do not know in advance the size your list will contain and an ArrayList contains Objects not based type like int, float, double, boolean, char, ....
You can avoid the overhead of object creation.

You can also avoid the +2 and -2 by making

int[] list = new int[13];

and just ignore what is in list[0] and list[1].

This post has been edited by pbl: 5 Jun, 2008 - 05:41 PM
User is online!Profile CardPM
+Quote Post

cutegrrl
RE: Using An ArrayList<Integer> Of Counters To Keep Track Of The Nu
4 Jun, 2008 - 02:29 PM
Post #4

D.I.C Head
**

Joined: 12 May, 2008
Posts: 72



Thanked: 7 times
My Contributions
QUOTE(pbl @ 4 Jun, 2008 - 01:51 PM) *

Cutegrrl is right. In your case using an ArrayList is like using a limo to deliver a pizza.

You can also avoid the +2 and -2 by making

int[] list = new int[13];

and just ignore what is in list[0] and list[1].


But you're making the array bigger than it has to be--and you're wasting space on those two unused elements. That's like ordering an extra-large pizza that you know you can't finish. LOL

I'm just kidding pbl. smile.gif In practice, the extra space usage is negligible and the code slightly easier to understand that way. smile.gif

This post has been edited by cutegrrl: 4 Jun, 2008 - 02:31 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Using An ArrayList<Integer> Of Counters To Keep Track Of The Nu
4 Jun, 2008 - 02:36 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(cutegrrl @ 4 Jun, 2008 - 03:29 PM) *

But you're making the array bigger than it has to be--and you're wasting space on those two unused elements. That's like ordering an extra-large pizza that you know you can't finish. LOL

I'm just kidding pbl. smile.gif In practice, the extra space usage is negligible and the code slightly easier to understand that way. smile.gif

I hope you are just kidding.....
Looked at the code generated( just for fun) ... your +2 and -2 generates more code than the 2 32 bits empty slots. So if you are really concerned about performance (because this is an Applet and the code is downloaded), my solution is better. smile.gif The array is allocated in the heap of the local machine so does not really impact performances.

Sorry I work on 64K board and a byte is a byte

This post has been edited by pbl: 5 Jun, 2008 - 12:15 PM
User is online!Profile CardPM
+Quote Post

C:/Syntax
RE: Using An ArrayList<Integer> Of Counters To Keep Track Of The Nu
5 Jun, 2008 - 07:05 AM
Post #6

New D.I.C Head
*

Joined: 12 Apr, 2008
Posts: 23



Thanked: 1 times
My Contributions
QUOTE(cutegrrl @ 4 Jun, 2008 - 12:40 PM) *

java


import java.util.*;

public class Dice {

private static Random r = new Random();
private static int[] list = new int[11];


public static void main(String[] args) {

roll(10);

for(int i = 0; i < list.length; i++){
System.out.println("Sum " + (i+2) + ": " + list[i]);
}

}


public static void roll(int n){
for (int i = 0; i < n; i++) {
list[((r.nextInt(6) + 1) + (r.nextInt(6) + 1))-2]++;
}
}

}



is this method for the the Applet, or class to roll Dice. I already have my own Dice class. I need help in the Applet, putting each occurrence in the ArrayList
User is offlineProfile CardPM
+Quote Post

pbl
RE: Using An ArrayList<Integer> Of Counters To Keep Track Of The Nu
5 Jun, 2008 - 07:43 AM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(C:/Syntax @ 5 Jun, 2008 - 08:05 AM) *

QUOTE(cutegrrl @ 4 Jun, 2008 - 12:40 PM) *

java


import java.util.*;

public class Dice {

private static Random r = new Random();
private static int[] list = new int[11];


public static void main(String[] args) {

roll(10);

for(int i = 0; i < list.length; i++){
System.out.println("Sum " + (i+2) + ": " + list[i]);
}

}


public static void roll(int n){
for (int i = 0; i < n; i++) {
list[((r.nextInt(6) + 1) + (r.nextInt(6) + 1))-2]++;
}
}

}





is this method for the the Applet, or class to roll Dice. I already have my own Dice class. I need help in the Applet, putting each occurrence in the ArrayList


cutegrrl just wrote an example on how to do it...

In your DiceRoll class replace:

CODE

   private ArrayList <Integer> sumList;
by
   private int[] sumList = new int[13];

and remove:

        sumList = new ArrayList<Integer>();
        for(int i=0; i<=13; i++)
        {
            sumList.add(0);
        }


After you calculated your sum add

CODE

   sumList[sum]++;


In you class Dice for which we do not have the source add

CODE

private static Random r = new Random();


and its roll method will be
CODE

int roll() {
    return r.nextInt(6) + 1;
}



User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:22PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month