473,379 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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 5842
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

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

Similar topics

1
by: aemazing | last post by:
hello, im new to the forum and i wanted to help with my c++ program. the teacher wants us to design a progrm that would keep track of airplanes awaitin landing at an airport. the program will...
1
by: aemazing | last post by:
i've been tryin to do the following - -Add a new flight number to the end of the queue (got it done) -LAnd the plane at the front of the queue - problems wit it- -display the queue - got it done...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
4
by: chaz | last post by:
here is the html : <br> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="<%=LABEL_WIDTH%>" class="formtext"><%= HTEXT("Connection type:")%></td> <td...
10
by: javuchi | last post by:
I just want to share some code with you, and have some comments and improvements if you want. This header file allocates and add and delete items of any kind of data from a very fast array: ...
5
by: desktop | last post by:
I have a function that takes two pointers to an array. The first point to the first element while the other points to the last element. int nums = { 1, 2, 3, 4, 5, 7, 8, 9}; int* result; int...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.