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

ArrayList

Citra Dil
Greenhorn

Joined: Mar 18, 2011
Posts: 22
How to rewrite the below code using ArrayList?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

What have you tried?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Basically, the steps are to create a new ArrayList, then add your elements. Note that an ArrayList cannot hold primitives, so these will probably need to be wrapped as Integers. Of course, you will also need to rewrite the setLocationCells method to take an ArrayList instead of an array.

Refer to the API documentation for java.util.ArrayList.

Give it a try, and post your code if you need help!


"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
Citra Dil
Greenhorn

Joined: Mar 18, 2011
Posts: 22
I have a string arraylist declared


I dont know how to assign values to ArrayList.I have no idea how to assign directly as in, int[] locations = {randomNum, randomNum+1, randomNum+2};
I have tried sometthing like this but that is not I wanted
ArrayList loc = new ArrayList();
loc.add("1");
loc.add("2");
loc.add("3")

Adam Juszkiewicz
Greenhorn

Joined: Mar 31, 2011
Posts: 3

I don't know if it's what you want to achieve, but if you want to create and fill list with values in one line (as you did with table), you should do something like this:

Citra Dil
Greenhorn

Joined: Mar 18, 2011
Posts: 22
thanks.
I've come across with using 'List' when googling.But is there any solution without importing these packages java.util.List, java.util.Arrays?

Adam Juszkiewicz
Greenhorn

Joined: Mar 31, 2011
Posts: 3

it won't work without imports
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Citra Dil wrote:I have tried sometthing like this but that is not I wanted
ArrayList loc = new ArrayList();
loc.add("1");
loc.add("2");
loc.add("3")



And why don't you want to do that?
Citra Dil
Greenhorn

Joined: Mar 18, 2011
Posts: 22
Hi Paul,
I want to avoid manually inserting numbers.According to my program logic I wanted it to be created random that too consecutive three numbers.
Also my ArrayList is declared as String(please ref my code earlier).later in my program I want to use locationCells.remove,locationCells.isEmpty etc.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Citra Dil wrote:I want to avoid manually inserting numbers.


I'm sorry, I don't understand what you mean by "manually".
Citra Dil
Greenhorn

Joined: Mar 18, 2011
Posts: 22
I meant, I would like to use random()
Adam Juszkiewicz
Greenhorn

Joined: Mar 31, 2011
Posts: 3

But using random doesn't prohibit you to use code that we wrote in posts before.

You can just assign random value to some local variable and add it to list. No problem.
Citra Dil
Greenhorn

Joined: Mar 18, 2011
Posts: 22
Marc,Adam thanks for your guidance.

I have tried the below code but getting a null point Exception

also I would like to know how to rewrite the setLocationCells method to take an ArrayList.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

If you wanted something like the first piece of code you posted then you could use a for loop like
Then you won't be "manually" adding each number (if that's what you meant).

Sean


I love this place!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList