wood burning stoves 2.0*
The moose likes Java in General and the fly likes ArrayList: How do you retrieve an item in an ArrayList and use that item for another purpose? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList: How do you retrieve an item in an ArrayList and use that item for another purpose?" Watch "ArrayList: How do you retrieve an item in an ArrayList and use that item for another purpose?" New topic
Author

ArrayList: How do you retrieve an item in an ArrayList and use that item for another purpose?

Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
Hey all:

I've been working on a project with ArrayList but I have been having problems with getting it to work. What I have right now is code that will tell the program to save 7 arrays in an ArrayList in one method.

What I want to do is extract one or more arrays out of the List and use them for another purpose in the program. I also have created another method that will attempt to extract a specific array from the list, but so for unfortunately, I have not been successful. :'(

Here is my code thus far:



That is the code from the first method that will store the seven arrays into the ArrayList. Now here is the second method that is to extract one array out of that list.



Now I have written a main method to experiment and see if this works. Here's the code for that:



But every time I run the main method, I unfortunately get this error instead-



Does anyone have any ideas of what the problem is or what I am doing wrong? Any suggestions for improvement? I apologize if this may seem a little lengthy.
Any help would be greatly appreciated.

Please let me know if you need further information on the program.

Thanks everyone!

SAM
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

The problem is that in your ExperimentMain class, on line 9, you refer to something called "Peri_Aph_Data_Generator", but the jvm has no idea what that is. Looking at your code, I don't know what that is, since you haven't defined it anywhere.

What is that supposed to be?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
Peri_Aph_Data Generator is the class that the two methods are in.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

Sam Thompson wrote:Peri_Aph_Data Generator is the class that the two methods are in.

Don't you think it would have been a good idea to mention that earlier?

Clearly the compiler can't figure that out. Have you compiled that class? Did it work? Is your classpath set up correctly?

What is your directory structure?
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
It did work. The class did compile and Netbeans didn't have any problems with it.
Here is the entire class if you need it.



Let me know what you find out .
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

I found out that the code you provided will not compile. I've fixed 3-4 errors so far.

I corrected the obvious errors in both your Peri_Aph_Data_Generator and your ExperimentMain classes.

once they both compiled, i was able to run the ExperimentMain class and got no errors.

Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
What and where were the errors were exactly?

What exactly wouldn't compile?

The Peri_APh... class compiled fine and the Main did too...
Were you able to extract one (or more) arrays out of the list?


Hmmm...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
"get" misspelt "gte".
The import statements are incorrect, except the first.
You really ought to use control-C control-V for posting real code.
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
Thanks for the tip...

I did copy and paste the code I have and then clicked on the code button.

The Calendar and Date packages I am importing will be used for the calculations of something else shortly- and may be implemented into the methods just shown. Since much of this relates to what time of the year it is and such.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

yes, but probably not what you want:

C:\slop>java ExperimentMain
[D@addbf1
[D@42e816
[D@9304b1
[D@190d11
[D@a90653
[D@de6ced
[D@c17164
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
Yeah definetly not. :P

What code would you put in there to actually spit out the generated data from that specific array? I know it exists but I don't know how to create or write it exactly.

Thanks a bunch.

PS: You said there were 3 or 4 errors. What and where were they?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

several import statement errors in both files
gte instead of get

An array (or an ArrayList, or really any container) is just that - a container. Assuming you have a container named "myList", if you say:

System.out.println(myList);

what you are saying is "print the name of the container" and not "print the name of each thing in the container".

Since you have a container of containers, your "data.get(i)" is returning a container, so you print its name.

once you get your container, you need to print the name of each element in it.

Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
The problem is finally resolved.

Thank you very much for your assistance.

It is greatly appreciated.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
... and how did you resolve it? We would like to know, and it might be helpful for others who have similar problems in future.
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
Sure I will be happy to

What the point of this program was, was to generate 100 different data points for the timespan of 100 years. So this meant I had to create an array for EACH planet, as you see in my code above, and then to be able to store them and use them for another part of the program. This meant using an ArrayList.

The problem I realized I was having was that not only because of silly mundane errors like having too many imports or misspelling accessor methods, but I realized I was not being specific enough in what I wanted the program to do.

What does this mean?

This means that, if you look at my code for MainTester, I was telling Java to print out the planet array names and NOT the calculated elements inside the arrays like I wanted. So intead of what you have above, what I really needed was this...



This tells the computer to step through each array in the list and print out every element in each array.

That's what I realized what I was doing wrong. I usually have the computer print out the result so that way I know either what's coming out is correct or not. In the programs I write, I usually don't need it to print it out.

I hope this is clear to understand. And I hope this helps for those who are or were having the same problem.

Again, thank you for helping me. I really appreciate it.

S.T.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Yes, it was clear.
And ... you're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList: How do you retrieve an item in an ArrayList and use that item for another purpose?
 
Similar Threads
Enum problem/error
Loosing Grey Matter
Changing Labels on an Checkbox Group
Need help with static methods.