File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes I need help on arrays and array lists! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I need help on arrays and array lists! " Watch "I need help on arrays and array lists! " New topic

I need help on arrays and array lists!

Kay Jay

Joined: Nov 16, 2009
Posts: 2
Hey everyone. I am trying to finish up a lab for my first java class ever, and boy am I struggling. I am trying to convert a method of arrays over to an array list. And while it seems like it should be simple, for whatever reason I am struggling. Any help would be great. Here's the method I have to re-write into an array list:

And here's my (failed attempt) at code so far:

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688


You usually access elements of a List via the get(int index) method.

[My Blog]
All roads lead to JavaRanch
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Its usually cleaner to get the element you need from the list and store it to a local variable. Also helps you to avoid accessing the list multiple times. As said above, use the get() method of the list:

Also, check the the array example you're comparing to some value nextPos, while in the Arraylist part you compare to the list size.
It is sorta covered in the JavaRanch Style Guide.
subject: I need help on arrays and array lists!