3 Replies - 59685 Views - Last Post: 21 November 2008 - 02:24 AM Rate Topic: -----

#1 wartech  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

how do you clear out elements in array Java

Posted 20 November 2008 - 11:39 PM

Hi Everyone,
I am having trouble with my program when i choose to reset the values in my program. I have two arrays and would like to clear the contents. The only way I have figured out how to do this is to create another array with the same amount of elements, initialize each value to zero and assign the array to each of the arrays that need to be cleared/reset.
It will show the output like I want it, but when it comes to simulating more dice being rolled, my arrays are not filling as they should. Any help would be appreciated. Thanks in advance for looking over.

private void resetBtnActionPerformed(java.awt.event.ActionEvent evt) {										 
   int resetArr[] ={0,0,0,0,0,0,0,0,0,0,0,0,0,0};
   diceArr = resetArr;
   freq = resetArr;
   DecimalFormat df = new DecimalFormat("#0.00");
   String output = "Possibility \t Percent \t Frequency \n\n";
   for (int index = 2; index < 13; index++) 
   {
		 num = (double)resetArr[index]/rolls;
		 output += index + "\t" + df.format(num * 100) + "%" + "\t" + resetArr[index]+ "\n";				 
   }
   jTextArea1.setText(output);	
} 


private void simBtnActionPerformed(java.awt.event.ActionEvent evt) {									   

		Random r = new Random(); 
		String output = "Possibility \t Percent \t Frequency \n\n";	  
		rolls = Integer.parseInt(numTxt.getText());
		for (int index = 0; index < rolls; index++)
		{
			int d1 = r.nextInt(6) + 1;
			int d2 = r.nextInt(6) + 1; 
			freq[d1 + d2] += 1; 

		}
		DecimalFormat df = new DecimalFormat("#0.00");
		for (int ctr = 2; ctr < 13; ctr ++)
		{
			output += diceArr[ctr] + "\t" + df.format(((double)freq[ctr] / rolls) * 100) + "%" + "\t" + freq[ctr]+ "\n";
		}
		
		jTextArea1.setText(output);
}




Is This A Good Question/Topic? 0
  • +

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: how do you clear out elements in array Java

Posted 21 November 2008 - 01:11 AM

import java.util.Arrays;
...
Arrays.fill(diceArr, 0);
...


This post has been edited by n8wxs: 21 November 2008 - 01:17 AM

Was This Post Helpful? 1

#6 wartech  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Re: how do you clear out elements in array Java

Posted 21 November 2008 - 02:16 AM

Thanks n8wxs!
I ended up using your code but on the freq array instead. Worked perfect. Thanks!

private void resetBtnActionPerformed(java.awt.event.ActionEvent evt) {										 
 
   Arrays.fill(freq, 0);

   DecimalFormat df = new DecimalFormat("#0.00");
   String output = "Possibility \t Percent \t Frequency \n\n";
   for (int index = 2; index < 13; index++) 
   {
		 num = (double)freq[index]/rolls;
		 output += index + "\t" + df.format(num * 100) + "%" + "\t" + freq[index]+ "\n";				 
   }
   jTextArea1.setText(output);	
}  


Was This Post Helpful? 0
  • +
  • -

#7 UltraFlynn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 136
  • Joined: 03-August 07

Re: how do you clear out elements in array Java

Posted 21 November 2008 - 02:24 AM

As an aside, the quickest way to copy an array is to use:

System.arraycopy(...)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1