Hey guys, I'm doing this really complex problem from a past exam paper (got my real exam next monday!!!) which requires us to store thousands of data into array lists and do some calculations with them like mean, standard deviation etc...

The data is to be extracted from a URL - this I can do easily.

The data in the URL contains approximately 10,000 values individually for x (height), y (weight) and another column for Group.

(Sorry I can't post the link here since its an internal university link which can only be accessed by students....)

Rough Example...

Group, x (height, metres), y (weight, kg)

1, 1.61, 77

2, 1.56, 56

3, 1.58, 66

4, etc...etc...

Now heres the catch: some x and y values have the same Group numbers so:

Group 1 has some x and y values associated with it, Group 2 has some x and y values associated with it and so on...There are 100 Groups in total. But the thing is, the whole data set in the URL is not in order....

Now how do I store all of this data in a single N x 3 array and calculate the mean x and y values for each Group 1,2,3,4,5 -> 100 ?

Heres some data points for visualisation:

Group, x, y

2, 1.44, 56

2, 1.55, 66

3, 1.65, 65

6, 1.83, 85

6, 1.71, 78

3, 1.78, 89

3, 1.67, 67

3, 1.49, 52

5, 1.71, 72

5, 1.52, 61

8, 1.98, 103

8, 1.67, 90

1, 1.76, 85

Any help or advice would be greatly appreciated.

Many thanks in advance.

## 4 Replies - 2462 Views - Last Post: 10 November 2012 - 06:13 AM

### #1

# How do I store thousands of data into arraylists and use them?

Posted 08 November 2012 - 10:13 AM

##
**Replies To:** How do I store thousands of data into arraylists and use them?

### #2

## Re: How do I store thousands of data into arraylists and use them?

Posted 08 November 2012 - 11:10 AM

You say there are 100 groups in total, does this mean the group numbers are from 1-100 ?

I bet you read in the data one line at a time? If you do this, you will know the first token is the group number

When you have the group number, you can find the corresponding Group by groups[groupNumber-1];

You could then make a Group class to carry the data

The Group class could also contain logic on how to find the mean etc.

Group[] groups = new Group[100];

I bet you read in the data one line at a time? If you do this, you will know the first token is the group number

When you have the group number, you can find the corresponding Group by groups[groupNumber-1];

You could then make a Group class to carry the data

The Group class could also contain logic on how to find the mean etc.

public class Group { private int groupNumber; private ArrayList<Double> heights; //m private ArrayList<Integer> weights; //kg public Group(int groupNumber) { this.groupNumber = groupNumber; heights = new ArrayList<Double>(); weights = new ArrayList<Integer>(); } public void addHeight(double height) { } public void addWeight(double weight) { } public double calculateMeanHeight() { } }

### #3

## Re: How do I store thousands of data into arraylists and use them?

Posted 08 November 2012 - 11:30 AM

CasiOo, on 08 November 2012 - 11:10 AM, said:

You say there are 100 groups in total, does this mean the group numbers are from 1-100 ?

I bet you read in the data one line at a time? If you do this, you will know the first token is the group number

When you have the group number, you can find the corresponding Group by groups[groupNumber-1];

You could then make a Group class to carry the data

The Group class could also contain logic on how to find the mean etc.

Group[] groups = new Group[100];

I bet you read in the data one line at a time? If you do this, you will know the first token is the group number

When you have the group number, you can find the corresponding Group by groups[groupNumber-1];

You could then make a Group class to carry the data

The Group class could also contain logic on how to find the mean etc.

public class Group { private int groupNumber; private ArrayList<Double> heights; //m private ArrayList<Integer> weights; //kg public Group(int groupNumber) { this.groupNumber = groupNumber; heights = new ArrayList<Double>(); weights = new ArrayList<Integer>(); } public void addHeight(double height) { } public void addWeight(double weight) { } public double calculateMeanHeight() { } }

Hi thanks for your reply.

Right now I'm using a for loop to sort the Groups out. Is this the right thing to do?

for(int i = 0; i < arrayGroup.size(); i++){ if(!arrayNewGroup.contains(arrayGroup.get(i))){ // arrayGroup.get(i) gets the position of i in the arraylist arrayNewGroup.add(arrayGroup.get(i));

I have created another class called Mean and developed a method called calculateMean.

How do I go about calculating the mean x and y values for each Group from here?

CasiOo, on 08 November 2012 - 11:10 AM, said:

You say there are 100 groups in total, does this mean the group numbers are from 1-100 ?

Group[] groups = new Group[100];

Forgot to say - yes there are 100 groups but the data is arranged in a way such that some x and y values may belong to the same group.

Example:

Group, x, y

1....

1

1

1

1

3

3

3

3

3

5

5

5

5

6

6

6

2

2

2

etc.

### #4

## Re: How do I store thousands of data into arraylists and use them?

Posted 08 November 2012 - 11:35 AM

Did you read my reply ?

groups[0] will be group 1

Every time group 1 occurs, you should take groups[0] and add the data to it

groups[0] will be group 1

Every time group 1 occurs, you should take groups[0] and add the data to it

### #5

## Re: How do I store thousands of data into arraylists and use them?

Posted 10 November 2012 - 06:13 AM

CasiOo, on 08 November 2012 - 11:35 AM, said:

Did you read my reply ?

groups[0] will be group 1

Every time group 1 occurs, you should take groups[0] and add the data to it

groups[0] will be group 1

Every time group 1 occurs, you should take groups[0] and add the data to it

I honestly still can't understand this even now and feel lost now and my exam is on Monday and I'm getting too stressed out..

I guess its probably my fault that i can't even get my head around even such simple concepts.

Maybe programming is not for me...

Page 1 of 1