aspose file tools*
The moose likes Beginning Java and the fly likes using array list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using array list" Watch "using array list" New topic
Author

using array list

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i have to do a remake of my last assignment, but using array list.

i have gotten this code down pretty well as it is basically a repeat



the confusing part is here. i have already made my arraylist and have plugged in all of the info. im trying to invoke the methods, but its a bit confusing.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39862
    
  28
Your CO2 List doesn't have "total" or calcNetWasteReduction methods. You will have to create those methods and (probably) pass the List to them.

But don't do it from inside the main method. Make the List a field, set it up in the constructor, and populate it, maybe also in the constructor. Then you will have it available for the total() or other methods.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

You probably should change line 14 of your second class to this:

It's better practice to code against an interface rather than an implementation. Also, you don't generally need to specify the size of a List; they can grow (and shrink) dynamically.

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
@ john, alright i have gotten this up to date.

@ campbell, would i have to do all of that? i did a program similar to this, but with an array of objects and i just had my data in the tester class and all of my methods in my regular class. ex:


would a similar approach not work with the array list?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

This application keeps coming back to bite us....

I would have used an ArrayList declared exactly as John said.

Your current approach would work the same as with an ArrayList, but Campbell is giving you tips on making your main() method smaller. Think about what he's saying and why it's more efficient (and I would think OO) than what you're doing.

When you do things right, people won't be sure you've done anything at all.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
alright, im feeling you. but, wouldnt i still invoke the methods in the main? im actually following examples from my assignment, i think the goal of the assignment is to kind of introduce the concepts, im sure efficiency will come later. lol i keep looking at this:


and wishing i could get it to do this:

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

nathan gibson wrote:alright, im feeling you. but, wouldnt i still invoke the methods in the main? im actually following examples from my assignment, i think the goal of the assignment is to kind of introduce the concepts, im sure efficiency will come later. lol i keep looking at this:


and wishing i could get it to do this:



Why isn't that working for you?
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
because that is a different program. the one that you were referring to was a program i had to do previously. i got another assignment and it was basically to do the same thing with arraylist. i want to call the methods and have them calculate for each thing in the list. the thing that is holding me back is know exactly how to do that. something like this:

for ( int counter = 0; counter < 5; counter++ ){
[counter]CO2FromWaste.calcGrossWasteEmission;
[counter]CO2FromWaste.calcNetWasteReduction;
[counter]CO2FromWaste.total;
}

but clearly this is not supposed to work, but just an idea of what i want to do.

i might not have made any sense

John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

You probably should use a for-each loop, instead of the old style for loop. It'll be easier to maintain, should you need to change the number of objects you add.

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
alright, but i still have the problem of not knowing how to call the methods =\
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

You CAN!

An ArrayList is like an array on steroids.

Anything you can do with an array you can do with an ArrayList.... you just need to figure out the syntax for it.

To iterate through an ArrayList of objects and call methods on the object in each spot....

I'll give you pseudocode and you see if you can write the real stuff.....



EDIT - John's for/each idea is better. I GAVE you syntax for that in one of our threads........

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
just a note, i havent added the for each loop yet, im going to get my methods working first. alright i have been looking at examples and this is mostly the type of thing is see:


but i get the error cannot find symbol method calcGrossWasteEmission (int)
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

Here is a generic for-each loop:

So, instead of worrying about a counter, each object is returned so you can manipulate it. The loop is smart enough to know how long your Collection or array is. It will work with any class that can return an Iterator.

John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

nathan gibson wrote:
but i get the error cannot find symbol method calcGrossWasteEmission (int)


That's because the method doesn't take an int and you put counter in the parameter.

P.S. think about how dot-operators work and why this loop gives you the same result every time:
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
okay, to be sure that we are on the same page and i am looking for the right thing, i am looking for something like a counter to make the method iterate through my arrays, correct?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

The iteration is through the objects, not through the array. You need to find a way to access the object that is in that index.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i have found this...

it looks like it would be useful, but i dont know how i would use it with my methods.

listName.get(index);

is this on the right track?
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

If you use a for-each loop, you don't need to do any of this. It will iterate over each element of the Collection or array, so you don't need to do any get() methods. You can just call the methods from the object.

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
John de Michele wrote:Nathan:

If you use a for-each loop, you don't need to do any of this. It will iterate over each element of the Collection or array, so you don't need to do any get() methods. You can just call the methods from the object.

John.

i followed your example. i think that some of the stuff in my array might be named improperly. when i tried to compile i got that my first statement inside the loop wasnt a statement

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
okay. okay, sorry for all of the confusion and everything. i really do apologize. i think i have been describing the wrong thing im trying to do therefore confusing you guy's answers. but i really am sorry. i found a good example and did things this way and it is working fine so far.

John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

You really need to follow Java naming conventions. Java variables should begin with a lowercase character. You should change line 7 to read:

Change lines 14-22 to read

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
thank you. im slowly piecing it together. im printing some test output and im getting zeros.



Pounds of CO2
Household Waste Recycled Total Net
Index People Paper Plastic Glass Cans Emissions Reduction Emissions
0.000.000.000.000.000.000.000.000.000.000.000.000.000.000.00


im not worried about spacing at this point, just the values.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

I see since I've been gone you made some progress with John....

So I assume all zeros is the incorrect output?

I tried to compile and run your code.... it wouldn't compile. Maybe you made some other changes to the CO2Footprint class? I got a lot of method doesn't exist errors....

Anyways, I suspect that the problem is you're running methods on CO2FromWaste Objects, but they're supposed to be CO2Footprint objects? Or maybe the class posted above needs some changes.

Can you repost both classes that are for this project? The ones you are using to compile and get the all zero output?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

Campbell Ritchie wrote:Your CO2 List doesn't have "total" or calcNetWasteReduction methods. You will have to create those methods and (probably) pass the List to them.

But don't do it from inside the main method. Make the List a field, set it up in the constructor, and populate it, maybe also in the constructor. Then you will have it available for the total() or other methods.


Hmph.... that was Campbell.... telling us we'd have this problem right now....... hours ago. LOL.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

I'm curious as to why you keep falling back to doing the old-style for loops when you don't have to. There's absolutely no reason for doing lines 31-38 separately. In fact, the way you're doing it now, you're repeating yourself* for no gain. You might consider printing out your header before running through your loop. Since lines 34-36 do exactly what you're doing in lines 19-21, you should also consider combining your outputting with your calculations.

*There is an important principle in programming called the DRY Principle. DRY stands for Do not Repeat Yourself. If you're in a situation where you've got the same code copied-and-pasted around, you should refactor it into a method.

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
John de Michele wrote:Nathan:

I'm curious as to why you keep falling back to doing the old-style for loops when you don't have to. There's absolutely no reason for doing lines 31-38 separately. In fact, the way you're doing it now, you're repeating yourself* for no gain. You might consider printing out your header before running through your loop. Since lines 34-36 do exactly what you're doing in lines 19-21, you should also consider combining your outputting with your calculations.

*There is an important principle in programming called the DRY Principle. DRY stands for Do not Repeat Yourself. If you're in a situation where you've got the same code copied-and-pasted around, you should refactor it into a method.

John.


its not so much that im falling back, as that when im just trying to simply get things to work well enough to see why my numbers are wrong. im just going with what i know so that i dont have to deal with more unknowns. the more i do what im familiar with the more i can rule out as possible problems.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

You should try putting some System.out.println() expressions in your methods to be sure your variables are what you think they should be.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
nathan gibson wrote:its not so much that im falling back, as that when im just trying to simply get things to work well enough to see why my numbers are wrong. im just going with what i know so that i dont have to deal with more unknowns. the more i do what im familiar with the more i can rule out as possible problems.


This section of code probably has more to do with your output issues than anything else:

You need to reverse the order of your assignments. 'numPeople = numberOfPeople' is not the same as 'numberOfPeople = numPeople'.

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
John de Michele wrote:
nathan gibson wrote:its not so much that im falling back, as that when im just trying to simply get things to work well enough to see why my numbers are wrong. im just going with what i know so that i dont have to deal with more unknowns. the more i do what im familiar with the more i can rule out as possible problems.


This section of code probably has more to do with your output issues than anything else:

You need to reverse the order of your assignments. 'numPeople = numberOfPeople' is not the same as 'numberOfPeople = numPeople'.

John.


i did exactly what you said and reversed the order. made no difference.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

I took the liberty of copying your CO2FromWaste class into a sandbox project. None of the member variables that you are using in your constructor are being used in any of your methods. So, you'll still need to make the change I suggested (otherwise you'll still end up with zeroes being printed), but you'll also have to use those variables.

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
... what is a sandbox?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

http://en.wikipedia.org/wiki/Sandbox_%28software_development%29
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

Basically what I did was I created a scratch project in Eclipse, and copied your classes into it.

John.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Nathan:

So I fiddled with your constructor:


Run it, and see what you get.

John.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
hey you guys. i have gotten this program all together. thanks for all of the help. here is the code if your interested in the final outcome.



i do have one final question though that is kind of making me laugh. my output has a single number that refuses to line up, i know that it is an oversight on my part, but i have been looking at it so long i cant find it. i tried to quote my output so you guys would know what i was talking about, but it auto lines it up and it doesnt depict what im saying. its a very minor thing though, i just cant find it.

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
oh, okay, the print thing was a simple simple over sight, but once again, for all of you guys that helped me through this thank you. here is the 200% sure final product




Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12



Nice work!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using array list