469,289 Members | 2,341 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

Uses of Array List

Hey. I'm having some difficulty in Array lists. I have to find the average of all the numbers in an array list. That's my first task. My second one is to find the mode of all the numbers, meaning the number that shows up the most. And the third one is to find a standard deviation, which I will get to later.

Anyway, right now I'm doing Average.

Expand|Select|Wrap|Line Numbers
  1. public class Statistics
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         ArrayList <Integer> intList = new ArrayList <Integer>();
  6.         int avgTemp = 0;
  7.         int count1 = 0;
  8.  
  9.         for (int i = 0; i < intList.size(); i++)
  10.             {
  11.                 int b = (int) (Math.random() * 10);
  12.                 intList.set(i, b);
  13.             }
  14.  
  15.         for (int i = 0; i < intList.size(); i++)
  16.             {
  17.                 int j = intList.get(i);
  18.                 avgTemp += j;
  19.                 count1++;
  20.             }
  21.  
  22.         System.out.println("The average of all the values in the Array List is " + avgTemp/count1);
  23.  
That's what I have so far. The first for loop makes a random array list, but I think it's wrong. And the second one will store all the integers of the arrayList into avg temp, and then I'll get the average in my output statement. But it doesnt work.
Dec 20 '07 #1
22 5470
BigDaddyLH
1,216 Expert 1GB
That's what I have so far. The first for loop makes a random array list, but I think it's wrong.
Greetings! Let's start with the first loop. Why do you think it's wrong? How can you prove or disprove your suspicions?
Dec 20 '07 #2
Expand|Select|Wrap|Line Numbers
  1. import java.util.ArrayList;
  2.  
  3. public class Statistics
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         ArrayList <Integer> intList = new ArrayList <Integer>();
  8.  
  9.         int size = intList.size();
  10.         for (int k = 0; k < size); k++)
  11.             {
  12.                   intList.add((int)(Math.random()*100));
  13.             }
  14.  
  15.         double count1 = 0;
  16.         for (int i = 0; i < intList.size(); i++)
  17.             {
  18.               count1 += intList.get(i);
  19.             }
  20.  
  21.         double average = (count1 / (intList.size()));
  22.  
  23.         System.out.println("The average of all the integers in the Array List is " + average);
  24. }
  25. }
  26.  
I modified it a little but it still doesn't work. In this one, I don't understand why it won't work. It returns "NaN"...
Dec 20 '07 #3
BigDaddyLH
1,216 Expert 1GB
NaN means "Not a number". A double gets this value if you perform an illegal operation like taking the square root of a negative number. Look at the defintion of average:
Expand|Select|Wrap|Line Numbers
  1. double average = (count1 / (intList.size()));
print out the value of count1 and of intList.size() -- what are their values?
Dec 20 '07 #4
NaN means "Not a number". A double gets this value if you perform an illegal operation like taking the square root of a negative number. Look at the defintion of average:
Expand|Select|Wrap|Line Numbers
  1. double average = (count1 / (intList.size()));
print out the value of count1 and of intList.size() -- what are their values?
oh

count is 0.0

and intList is 0

but why? I am adding random numbers to the Array List. and the () at the end of the declaration of the Array List should equal 10. So intList.size() should be 10. and count should be all the random numbers added together....
Dec 20 '07 #5
BigDaddyLH
1,216 Expert 1GB
oh

count is 0.0

and intList is 0

but why? I am adding random numbers to the Array List. and the () at the end of the declaration of the Array List should equal 10. So intList.size() should be 10. and count should be all the random numbers added together....
I don't understand what you mean by "and the () at the end of the declaration of the Array List should equal 10".

1. How many numbers are you adding to intList?
2. What is the size of intList after you execute:

Expand|Select|Wrap|Line Numbers
  1. ArrayList <Integer> intList = new ArrayList <Integer>();
Dec 20 '07 #6
I don't understand what you mean by "and the () at the end of the declaration of the Array List should equal 10".

1. How many numbers are you adding to intList?
2. What is the size of intList after you execute:

Expand|Select|Wrap|Line Numbers
  1. ArrayList <Integer> intList = new ArrayList <Integer>();
I thought that by default ArrayList <integer> intList = new ArrayList <integer>() has the size of 10....

So how do I set the size?
Dec 20 '07 #7
BigDaddyLH
1,216 Expert 1GB
I thought that by default ArrayList <integer> intList = new ArrayList <integer>() has the size of 10....

So how do I set the size?
I think you are confusing size with capacity. Capacity is an implementation detail that can be safely ignored. When you create an ArrayList with that constructor, its initial size is 0. It is empty.

The easieest way to increase that list to size 10 is to add 10 values to it! In your first loop, why not use an upper bound of 10 instead of intList.size()?
Dec 20 '07 #8
I think you are confusing size with capacity. Capacity is an implementation detail that can be safely ignored. When you create an ArrayList with that constructor, its initial size is 0. It is empty.

The easieest way to increase that list to size 10 is to add 10 values to it! In your first loop, why not use an upper bound of 10 instead of intList.size()?

Ooh, thanks. I got it working.

The next part of my program needs to find the Standard Deviation of the Array List.

This is how to find it


a. Find the average of the list of numbers.
b. Determine the difference of each number from the average, and square each difference. Sum all the differences.
c. Divide this sum by (the number of values - 1).
d. Take the square root of the above division result from step c.

Example, given this list of numbers: 7 4 5 9 10

a. The average = 7
b. Sum of square of differences:

(7 - 7)2 + (4 - 7)2 + (5 - 7)2 + (9 - 7)2 + (10 - 7)2
0 + 9 + 4 + 4 + 9 = 26

c. 26(5-1) = 6.50

d. 6.50 = 2.55


So, I'm gonna give a shot at it now. And I'll see how I do, if you guys have any ideas, please let me know
Dec 20 '07 #9
I did the Standard Deviation, but I want to round this off to 2 decimal places

Expand|Select|Wrap|Line Numbers
  1. import java.util.ArrayList;
  2. import java.lang.Math;
  3.  
  4. public class Statistics
  5. {
  6.     public static void main(String args[])
  7.     {
  8.         ArrayList <Integer> intList = new ArrayList <Integer>();
  9.         double count1 = 0.0;
  10.         double sdTemp = 0.0;
  11.  
  12.         for (int k = 0; k < 10; k++)
  13.             {
  14.                   intList.add((int)(Math.random()*10));
  15.             }
  16.  
  17.         for (int i = 0; i < intList.size(); i++)
  18.             {
  19.               count1 += intList.get(i);
  20.             }
  21.  
  22.         double average = (count1 / (intList.size()));
  23.  
  24.         System.out.println("The average of all the integers in the Array List is " + average);
  25.  
  26.         for (int j = 0; j < intList.size(); j++)
  27.             {
  28.                 int q = intList.get(j);
  29.                 sdTemp += Math.pow((q - average), 2);        
  30.             }
  31.  
  32.         double sdTemp2 = (sdTemp / (intList.size() - 1));
  33.         double standardDeviation = Math.sqrt(sdTemp2);
  34.  
  35.         System.out.println("The Standard Deviation is " + standardDeviation);
  36.  
Dec 20 '07 #10
Heh, never mind, I got it.

Okay, now here is the hardest part

I need to find the mode of the Array List, meaning the number that appears the most...

Expand|Select|Wrap|Line Numbers
  1. import java.util.ArrayList;
  2. import java.lang.Math;
  3. import java.text.DecimalFormat;
  4.  
  5. public class Statistics
  6. {
  7.     public static void main(String args[])
  8.     {
  9.         DecimalFormat df1 = new DecimalFormat("####.00");
  10.         ArrayList <Integer> intList = new ArrayList <Integer>();
  11.         double count1 = 0.0;
  12.         double sdTemp = 0.0;
  13.  
  14.         for (int k = 0; k < 10; k++)
  15.             {
  16.                   intList.add((int)(Math.random()*10));
  17.             }
  18.  
  19.         for (int i = 0; i < intList.size(); i++)
  20.             {
  21.               count1 += intList.get(i);
  22.             }
  23.  
  24.         double average = (count1 / (intList.size()));
  25.  
  26.         System.out.println("The average of all the integers in the Array List is " + average);
  27.  
  28.         for (int j = 0; j < intList.size(); j++)
  29.             {
  30.                 int q = intList.get(j);
  31.                 sdTemp += Math.pow((q - average), 2);        
  32.             }
  33.  
  34.         double sdTemp2 = (sdTemp / (intList.size() - 1));
  35.         double standardDeviation = Math.sqrt(sdTemp2);
  36.  
  37.         System.out.println("The Standard Deviation is " + df1.format(standardDeviation));
  38.  
  39. }
  40. }
  41.  
This is what I have so far. If anyone could get me started, that would be helpfult
Dec 20 '07 #11
BigDaddyLH
1,216 Expert 1GB
How would you calculate the mode(s) by hand?

And can you make any assumptions about the range of the data values? You random number formula only generated numbers in the range 0-9. Can you assume that or do you have to assume any int is possible?
Dec 20 '07 #12
How would you calculate the mode(s) by hand?

And can you make any assumptions about the range of the data values? You random number formula only generated numbers in the range 0-9. Can you assume that or do you have to assume any int is possible?

from 0 - 99

I would calculate the mode by hand by doing:

Storing each value into a variable and just adding 1 to it everytime i pass it. Then i print out the one that has the highest number.

But doing it that way would take forever.
Dec 21 '07 #13
Ganon11
3,652 Expert 2GB
from 0 - 99

I would calculate the mode by hand by doing:

Storing each value into a variable and just adding 1 to it everytime i pass it. Then i print out the one that has the highest number.

But doing it that way would take forever.
That's not how you would do it by hand - that's a complicated method of solving the problem with a computer. Forget 'variables' and 'printing' - If I gave you a list of numbers on a piece of paper, and I gave you a pencil, could you write down the mode of that list of numbers? Figure out what you do to determine the mode in this way, find a way to generalize it (a.k.a. write a set of rules to be followed every time), and only then start coding.
Dec 21 '07 #14
That's not how you would do it by hand - that's a complicated method of solving the problem with a computer. Forget 'variables' and 'printing' - If I gave you a list of numbers on a piece of paper, and I gave you a pencil, could you write down the mode of that list of numbers? Figure out what you do to determine the mode in this way, find a way to generalize it (a.k.a. write a set of rules to be followed every time), and only then start coding.
I would tally each number that appeared and then see which one appeared the most.

To determine the mode, I have to see which number appears the most.
Dec 22 '07 #15
BigDaddyLH
1,216 Expert 1GB
I would tally each number that appeared and then see which one appeared the most.
It's starting to sound like an algorithm, because you've broken it down into two phases:
  1. Tally each number
  2. Then see which one(s) appears the most
Dec 22 '07 #16
It's starting to sound like an algorithm, because you've broken it down into two phases:
  1. Tally each number
  2. Then see which one(s) appears the most
Heh, yeah.

But my idea, in code, for it is to make different variables to store each number. But that would take forever.
Dec 22 '07 #17
BigDaddyLH
1,216 Expert 1GB
Heh, yeah.

But my idea, in code, for it is to make different variables to store each number. But that would take forever.
You need 100 variables, one each to hold the tally count for that data value. Now if there was only an easy way to declare variables in bulk... Hmmm.... anything ring a bell... declare 100 at a time...
Dec 23 '07 #18
You need 100 variables, one each to hold the tally count for that data value. Now if there was only an easy way to declare variables in bulk... Hmmm.... anything ring a bell... declare 100 at a time...

i was thinking about using an array.... would that help
Dec 23 '07 #19
BigDaddyLH
1,216 Expert 1GB
i was thinking about using an array.... would that help
Yes, indeed. Why not try using an array.
Dec 23 '07 #20
BigDaddyLH
1,216 Expert 1GB
Yes, indeed. Why not try using an array.
Although, if the point of this exercise is to use lists, you could also using a list like an ArrayList instead.
Dec 24 '07 #21
I see. Yeah, ArrayLists are much easier to use.
Dec 25 '07 #22
BigDaddyLH
1,216 Expert 1GB
I see. Yeah, ArrayLists are much easier to use.
My knee-jerk reaction is to use the collection framework over an array in all but trivial cases, because the collection framework is more flexible.

But even so, sometimes the array code is simpler:
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class Comparison {
  4.     private static final int MAX = 10;
  5.  
  6.     public static void main(String[] args) {
  7.         int[] v = new int[MAX]; //MAX zeroes
  8.         List<Integer> list = new ArrayList<Integer>(Collections. nCopies(MAX, 0)); //MAX zeroes
  9.  
  10.         //increment the values at odd offsets
  11.         for(int i=1; i<v.length; i+=2) {
  12.             v[i]++;
  13.         }
  14.  
  15.         //increment the values at odd offsets
  16.         for(int i=1; i<list.size(); i+=2) {
  17.             list.set(i, 1 + list.get(i));
  18.         }
  19.  
  20.         System.out.println(Arrays.toString(v));
  21.         System.out.println(list);
  22.     }
  23. }
Dec 26 '07 #23

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by aemazing | last post: by
1 post views Thread by aemazing | last post: by
8 posts views Thread by Peter B. Steiger | last post: by
10 posts views Thread by javuchi | last post: by
5 posts views Thread by desktop | last post: by
35 posts views Thread by Lee Crabtree | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.