• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList

 
Citra Dil
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to rewrite the below code using ArrayList?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Citra Dil
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it won't work without imports
 
Paul Clapham
Sheriff
Posts: 21113
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21113
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant, I would like to use random()
 
Adam Juszkiewicz
Greenhorn
Posts: 3
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic