Uses of Array List

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Energizer100
    New Member
    • Nov 2007
    • 60

    #1

    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.

    Code:
    public class Statistics
    {
    	public static void main(String args[])
    	{
    		ArrayList <Integer> intList = new ArrayList <Integer>();
    		int avgTemp = 0;
    		int count1 = 0;
    		
    		for (int i = 0; i < intList.size(); i++)
    			{
    				int b = (int) (Math.random() * 10);
    				intList.set(i, b);
    			}
    	
    		for (int i = 0; i < intList.size(); i++)
    			{
    				int j = intList.get(i);
    				avgTemp += j;
    				count1++;
    			}
    	
    		System.out.println("The average of all the values in the Array List is " + avgTemp/count1);
    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.
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by Energizer100
    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?

    Comment

    • Energizer100
      New Member
      • Nov 2007
      • 60

      #3
      Code:
      import java.util.ArrayList;
      
      public class Statistics
      {
      	public static void main(String args[])
      	{
      		ArrayList <Integer> intList = new ArrayList <Integer>();
      		
      		int size = intList.size();
      		for (int k = 0; k < size); k++)
      			{
        				intList.add((int)(Math.random()*100));
      			}
      	
      		double count1 = 0;
      		for (int i = 0; i < intList.size(); i++)
      			{
      			  count1 += intList.get(i);
      			}
      		
      		double average = (count1 / (intList.size()));
      		
      		System.out.println("The average of all the integers in the Array List is " + average);
      }
      }
      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"...

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #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:
        Code:
        double average = (count1 / (intList.size()));
        print out the value of count1 and of intList.size() -- what are their values?

        Comment

        • Energizer100
          New Member
          • Nov 2007
          • 60

          #5
          Originally posted by BigDaddyLH
          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:
          Code:
          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....

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by Energizer100
            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:

            [CODE=Java]ArrayList <Integer> intList = new ArrayList <Integer>();[/CODE]

            Comment

            • Energizer100
              New Member
              • Nov 2007
              • 60

              #7
              Originally posted by BigDaddyLH
              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:

              [CODE=Java]ArrayList <Integer> intList = new ArrayList <Integer>();[/CODE]
              I thought that by default ArrayList <integer> intList = new ArrayList <integer>() has the size of 10....

              So how do I set the size?

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by Energizer100
                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()?

                Comment

                • Energizer100
                  New Member
                  • Nov 2007
                  • 60

                  #9
                  Originally posted by BigDaddyLH
                  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

                  Comment

                  • Energizer100
                    New Member
                    • Nov 2007
                    • 60

                    #10
                    I did the Standard Deviation, but I want to round this off to 2 decimal places

                    Code:
                    import java.util.ArrayList;
                    import java.lang.Math;
                    
                    public class Statistics
                    {
                    	public static void main(String args[])
                    	{
                    		ArrayList <Integer> intList = new ArrayList <Integer>();
                    		double count1 = 0.0;
                    		double sdTemp = 0.0;
                    		
                    		for (int k = 0; k < 10; k++)
                    			{
                      				intList.add((int)(Math.random()*10));
                    			}
                    	
                    		for (int i = 0; i < intList.size(); i++)
                    			{
                    			  count1 += intList.get(i);
                    			}
                    		
                    		double average = (count1 / (intList.size()));
                    		
                    		System.out.println("The average of all the integers in the Array List is " + average);
                    		
                    		for (int j = 0; j < intList.size(); j++)
                    			{
                    				int q = intList.get(j);
                    				sdTemp += Math.pow((q - average), 2);		
                    			}
                    		
                    		double sdTemp2 = (sdTemp / (intList.size() - 1));
                    		double standardDeviation = Math.sqrt(sdTemp2);
                    		
                    		System.out.println("The Standard Deviation is " + standardDeviation);

                    Comment

                    • Energizer100
                      New Member
                      • Nov 2007
                      • 60

                      #11
                      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...

                      Code:
                      import java.util.ArrayList;
                      import java.lang.Math;
                      import java.text.DecimalFormat;
                      
                      public class Statistics
                      {
                      	public static void main(String args[])
                      	{
                      		DecimalFormat df1 = new DecimalFormat("####.00");
                      		ArrayList <Integer> intList = new ArrayList <Integer>();
                      		double count1 = 0.0;
                      		double sdTemp = 0.0;
                      		
                      		for (int k = 0; k < 10; k++)
                      			{
                        				intList.add((int)(Math.random()*10));
                      			}
                      	
                      		for (int i = 0; i < intList.size(); i++)
                      			{
                      			  count1 += intList.get(i);
                      			}
                      		
                      		double average = (count1 / (intList.size()));
                      		
                      		System.out.println("The average of all the integers in the Array List is " + average);
                      		
                      		for (int j = 0; j < intList.size(); j++)
                      			{
                      				int q = intList.get(j);
                      				sdTemp += Math.pow((q - average), 2);		
                      			}
                      		
                      		double sdTemp2 = (sdTemp / (intList.size() - 1));
                      		double standardDeviation = Math.sqrt(sdTemp2);
                      		
                      		System.out.println("The Standard Deviation is " + df1.format(standardDeviation));
                      
                      }
                      }
                      This is what I have so far. If anyone could get me started, that would be helpfult

                      Comment

                      • BigDaddyLH
                        Recognized Expert Top Contributor
                        • Dec 2007
                        • 1216

                        #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?

                        Comment

                        • Energizer100
                          New Member
                          • Nov 2007
                          • 60

                          #13
                          Originally posted by BigDaddyLH
                          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.

                          Comment

                          • Ganon11
                            Recognized Expert Specialist
                            • Oct 2006
                            • 3651

                            #14
                            Originally posted by Energizer100
                            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.

                            Comment

                            • Energizer100
                              New Member
                              • Nov 2007
                              • 60

                              #15
                              Originally posted by Ganon11
                              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.

                              Comment

                              Working...