ArrayList help, regarding objects in ArrayLists

Hie there,

How do I get a specific variable of an object which belongs to an arrayList.

Here's the workflow:

1) Program reads a text file line by line for information.

2) Extracts token by tokens and store in variables.

3) Instantiate a new class object with the different variables as parameters. (all up to here is using while loop)

4) Add the objects to an arraylist until line = null

5) Now that the objects are in place in the arraylist, how do i extract a specific data of an object for calculations? ( Assume that the specific data required applies to all the other objects) I used a For Loop to extract one by one:

for (int i=0; i<records.size(); i++){

how do i move on from there?

Main question: How do i call the method from the class file to the main file to retrieve the private variable in the class file using the for loop as stated above?>

[1018 byte] By [deathacka] at [2007-11-27 10:16:07]
# 1

It's not clear what you're asking, but if you want to retrieve a specific element "by name", use a Map.

http://java.sun.com/docs/books/tutorial/collections/

jverda at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 2

> for (int i=0; i<records.size(); i++){

By the way, this is the wrong way to extract items from a Collection one-by-one. Use an Iterator instead, or the enhanced for loop.>

jverda at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 3

import java.util.*;

public class ListExample {

public static void main(String[] args) {

List < Integer > list = new ArrayList < Integer > ();

for (int i=0; i < 10; ++i)

list.add(i);

int sum = 0;

for(Integer x : list) {

sum += x.intValue();

}

System.out.println(sum);

}

}

BigDaddyLoveHandlesa at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 4

Hi

the ArrayList has a get method, lets say your ArrayList is records

Object o = records.get(i);

retrieves the object at position i.

> Main question: How do i call the method from the

> class file to the main file to retrieve the private

> variable in the class file using the for loop as

> stated above?

Um, you want to retrieve a private (!) variable from another class?

You might want to use get/set methods which are public while your variable can safely stay private

class myObject extends whateverObject

{ private int v;

public void setValue(int i) {v = i;}

public int getValue() {return v;}

}

regards

Oliver

BugBunnya at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 5

Suppose you have this object inside the arrayList

public class SomeObject{

String someVar;

public String getSomeVar(){

return someVar;

}

public void setSomeVar(String someVar){

this.someVar = someVar;

}

}

To extract, using your notation, goes like this:

for (int i=0; i<records.size(); i++){

SomeObject someObject = (SomeObject)records.get(i);

//Do stuff with your extracted object, e.g.,

System.out.println(someObject.getSomeVar());

//Another way,

System.out.println(((SomeObject)records.get(i)).getSomeVar());

}

although, as mentioned, this is not the best approach and I'm not using generics.

Message was edited by:

manuel.leiria>

manuel.leiriaa at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 6

The thing here is i am confused myself.

Firstly, the object class (E.g. TaxRecord.java) is just a class with all the basic constructors and private variables. It also has methods such as toString( ) and another such as getIncome( ).

Secondly in the main class, (E.g. Main.java) I declared as:

TaxRecord tr;

The text file (e.g. TaxRecord.txt) contains data in this format:

<name>|<hp.no>|<address>|<income>|<donation>

So when the program runs, it reads the text file line by line and extracts every single token such as name hpNo and address which are stored in the variables under main class.

Then, I instantiate a new object like this:

tr = new TaxRecord (name, hpNo, address, income, donation);

Once that is done, i add the object to an arrayList named records so i add it this way.

records.add(tr)

What I'm confused about is, how do I call the methods in the TaxRecord.java class file where multiple objects are concerned?

For example I have 3 guys. Tom **** and Harry. Each and every one of them is an object of class TaxRecord. How do i extract ONLY the income information from Tom, **** and Harry in the main class?

I hope this clears out the doubts :S

Message was edited by:

deathack

deathacka at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 7

I think examples of this have already been posted in this thread, but is this clearer?

You have:

class TaxRecord {

public int getIncome() {

...

}

...

}

So you can write, using the popular enhanced for loop:

List < TaxRecord > records = new ArrayList < TaxRecord > ();

...

for(TaxRecord record : records) {

int income = record.getIncome();

...

}

BigDaddyLoveHandlesa at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 8

Do i need to import anything for the enhanced for loop? Coz i seem to be getting this error:

Main.java:132: not a statement

for (tr:records){

^

Main.java:132: ';' expected

for (tr:records){

^

Message was edited by:

deathack

deathacka at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 9

for (TaxRecord tpr:recs){

BigDaddyLoveHandlesa at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 10

> > for (TaxRecord tpr:recs){

>

wow, it worked! i deleted of what i declared on top which is:

TaxRecord tr;

and i declared as what u have posted:

(: Thank you!

But one more thing though. I also need to know how to extract one at a time and do calculations with it? For example i need 5% of the income as tax. so how do I come about doing that?

1) Extract income of Tom

2) Calculate the tax for Tom and store it in a variable

3) Repeat with other objects

deathacka at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 11

You've got the loop. Try to do the rest.

BigDaddyLoveHandlesa at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 12

> But one more thing though. I also need to know how to

> extract one at a time and do calculations with it?

> For example i need 5% of the income as tax. so how do

> I come about doing that?

>

> 1) Extract income of Tom

If you mean you want to get Tom directly, rather than when his turn comes up in the iteration, then, as I said before, use a Map.

jverda at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 13

I have got the loop but it's my first time using an enhanced for loop so I wouldnt know how to use it. Unlike the normal for loop with i as and index. I can just use i to manipulate the data freely. But the loop that you gave me, i doubt i can understand it fully. All i know is it prints out the income information for all the objects at once.

deathacka at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 14

If the syntax of the enhanced for loop makes you uneasy, these three loops do the same thing. Take your pick!

for(TaxRecord record : records) {

int income = record.getIncome();

//...

}

for(Iterator <TaxRecord > i = records.iterator(); i.hasNext(); ) {

TaxRecord record = i.next();

int income = record.getIncome();

//...

}

for(int j = 0; j < records.size(); ++j) {

TaxRecord record = records.get(j);

int income = record.getIncome();

//...

}

BigDaddyLoveHandlesa at 2007-7-28 15:43:38 > top of Java-index,Java Essentials,Java Programming...
# 15

> I have got the loop but it's my first time using an

> enhanced for loop so I wouldnt know how to use it.

> Unlike the normal for loop with i as and index. I can

> just use i to manipulate the data freely. But the

> loop that you gave me, i doubt i can understand it

> fully. All i know is it prints out the income

> information for all the objects at once.

What point are you trying to make here?

What you do inside the loop body has nothing to do with which form the loop control takes (except that there are some things you can't do with enhanced for because certain things are not there).

If you don't understand enhanced for, look here:

http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html

jverda at 2007-7-28 15:43:43 > top of Java-index,Java Essentials,Java Programming...
# 16

> If you mean you want to get Tom directly, rather than

> when his turn comes up in the iteration, then, as I

> said before, use a Map.

I googled about Map and it does make sense. However, I'm trying to get only one of Tom's information rather than Tom itself. So i do not want the entire object.

In the Map case, collection = m.values( ); looks good but it's just that i can't apply it to my needs. If it gets the collection of elements that Tom has, how do i get a specific element from Tom then using a Map? Does value mean the position of the element in an object?

deathacka at 2007-7-28 15:43:43 > top of Java-index,Java Essentials,Java Programming...
# 17

Whoa, we're starting to lose the intent of this thread.

Are you trying to do 1 or 2?

1. Given a bunch of records, look up tom's in particular and do something with it.

or

2. Given a bunch of records, go through them all and do something with each record in turn.

BigDaddyLoveHandlesa at 2007-7-28 15:43:43 > top of Java-index,Java Essentials,Java Programming...
# 18

2. Given a bunch of records, go through them all and do something with each record in turn.

Definitely the second option. Thank you for the clarification.

deathacka at 2007-7-28 15:43:43 > top of Java-index,Java Essentials,Java Programming...
# 19

> 2. Given a bunch of records, go through them all and

> do something with each record in turn.

>

> Definitely the second option. Thank you for the

> clarification.

Then just iterate. You don't need a map.

jverda at 2007-7-28 15:43:43 > top of Java-index,Java Essentials,Java Programming...
# 20

Guess what, you guys really helped me. I appreciate all the lightning quick replies and I feel i have got the hang of the problem already (:

Thank you so much! For loop ftw!

deathacka at 2007-7-28 15:43:43 > top of Java-index,Java Essentials,Java Programming...