• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help converting to ArrayList  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help converting my code into an ArrayList. We are only supposed to use an ArrayList and not an array. I didn't see that part of my project and not too good with using ArrayList. Please help me figure this out.


 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert Harp wrote:I need help converting my code into an ArrayList. We are only supposed to use an ArrayList and not an array. I didn't see that part of my project and not too good with using ArrayList. Please help me figure this out.

It's fairly straightforward:
  • Every place you have 'X[]', replace it with 'List<X>' (note: much better than 'ArrayList<X>').
  • Every place you have 'new X[n]', replace it with 'new ArrayList<X>(n)'.
  • Every place you have 'arrayname[n]' that is not followed by an '=' sign, replace it with 'listname,get(n)'.

  • The only other thing to deal with is initializing its values, and the easiest way to do that is to add them in the order you want. Have a look at the API docs for ArrayList.add() for details.

    HIH

    Winston
     
    Marshal
    Posts: 56610
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And welcome to the Ranch
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!