This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I am using the headfirst java book to learn Java and although they mention about ArrayLists and their usage, when i try to compile a program using arraylists the way they have asked us to, it does not. Can anybody help me in this please. Below is the code i tried to compile.
Originally posted by Neeraja Murali: I am using the headfirst java book to learn Java and although they mention about ArrayLists and their usage, when i try to compile a program using arraylists the way they have asked us to, it does not. Can anybody help me in this please
It would help us to answer your question if you post the specific errors you're getting. But my guess is that your code does not include a proper import statement. ArrayList is in the java.util package. Unlike java.lang, you need to explicitly import this to use in your program. So at the top of your code, be sure you have one of the following...
import java.util.*; //imports everything in the java.util package import java.util.ArrayList; //imports the ArrayList class
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
I can probably help you, as can many here, if you want to provide a code sample that you have that is not working correctly.
Without that sample, I can only make suggestions based on what I think you might be doing wrong.
Basically, when you want to replace an array with an array list, there are only a couple of issues to worry about:
The initialization: An array list does not HAVE to be initialized with a size, but it is often more efficient to do so, if you know what size it will be. Without a size, the constructor is newArrayList(); With a size, the constructor is newArrayList(size);
Value Assignment: To assign a value to the list, you can call ArrayList.add(Object), which automatically puts the object at the end of the occupied part of the array list. Or you can call ArrayList.add(index, Object), which performs exactly the same as array[index] = Object;
Value Accessing: To access a value from the list, replace array[index] with ArrayList.get(index);
That's pretty well all there is to it. If you are doing all that correctly, and still getting errors, post your code, and possible the stack trace of the exception, and I might be able to shed more light on the problem.
Thanks for replying to my query. I figured out that the problem arose becos although i had included JDK1.5/bin in the path variable, there also seemed to be some reference to an older version of java. This was the reason why my ArrayList program was not compiling.
Cheers Neeru
Neeraja Murali
Greenhorn
Joined: Jun 08, 2006
Posts: 4
posted
0
Hi Marc,Justin, Adam,& Campbell,
Thanks for replying to my query. I figured out that the problem arose becos although i had included JDK1.5/bin in the path variable, there also seemed to be some reference to an older version of java. This was the reason why my ArrayList program was not compiling.