• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding objects to an arralist  RSS feed

 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,

I was trying to make a class for an arraylist with setter and getter for the arrayilst so I can add objects from other classes to  the list without using statics. So how can I have an arraylist and add objects from other classes without setting it as statics?

Thanks you
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In pseudo code

Create custom class (say DataBag)
Create an instance variable to the class which is of type List
Provide a method say addData(Object newData) to this class
In the addData implementation add the newData to your list

From the external class
Create new instance
DataBag myDataBag = new ....
Then ise the addData method to add the data myDataBag.addData(Object newData);
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi saeid,
you mean to say you are required to create a class say xyz and their in you want to use an arraylist as a component where you can add or remove some stuff and along with the functionality of getters and setters in it.is it or something else? and what do you mean by "without using statics"?can you please explain a bit more.

Kind regards,
Praveen
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:Hi saeid,
you mean to say you are required to create a class say xyz and their in you want to use an arraylist as a component where you can add or remove some stuff and along with the functionality of getters and setters in it.is it or something else? and what do you mean by "without using statics"?can you please explain a bit more.

Kind regards,
Praveen


Yes that's right. A class like datalist that data and objects can be added from other classes and the arraylist should not be static in its own class.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:In pseudo code

Create custom class (say DataBag)
Create an instance variable to the class which is of type List
Provide a method say addData(Object newData) to this class
In the addData implementation add the newData to your list

From the external class
Create new instance
DataBag myDataBag = new ....
Then ise the addData method to add the data myDataBag.addData(Object newData);

I Think it is exactly what I need but can you please show me with example code if possible?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry
Ranch is NotACodeMill and you need to DoYourOwnHomework

I have given you sufficient pseudo code for you to write your own code. Give it a shot. If you are stuck anywhere, post your code here, tell us what problem you are facing and we will help point you in the right direction
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Sorry
Ranch is NotACodeMill and you need to DoYourOwnHomework

I have given you sufficient pseudo code for you to write your own code. Give it a shot. If you are stuck anywhere, post your code here, tell us what problem you are facing and we will help point you in the right direction

I think we have a bit of confusion here. I did not ask for the code for programme. Thanks for your help mate I'm already knew that much.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my Datalist

I'm trying to add by creating an object
Datalist list = new Datalist();
And then using list to add the data but I'm having a bit of trouble there.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote:
I'm trying to add by creating an object
Datalist list = new Datalist();
And then using list to add the data but I'm having a bit of trouble there.

What kind of trouble?
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how to add it. List.setslist().add (object)?
Or do I nees to have something else in my Datalist class.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setLists is the setter method. You need to use the getter method
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A nit about standard conventions for getters/setters...
Getter is getList() not getsList(). Likewise, setters are setList(...) not setsList(...).
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:A nit about standard conventions for getters/setters...
Getter is getList() not getsList(). Likewise, setters are setList(...) not setsList(...).

The list I created is called sList so the getter is caller getsList()
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, it should be getSList().
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:setLists is the setter method. You need to use the getter method

I try This this one as well but eclipse tells me to change the getter to static. What if I want my List to be private in its own class. Otherwise what is the point of having setter and getter I can simply declare the ArrayList as static and directly call in from another class.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eclipse is telling you that the getter should be static probably because you are calling it from a static method (maybe main?)  Don't do that.  Create an instance of your class in main and call a "driver" method from there.  See MainIsAPain (that's a link).
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Eclipse is telling you that the getter should be static probably because you are calling it from a static method (maybe main?)  Don't do that.  Create an instance of your class in main and call a "driver" method from there.  See MainIsAPain (that's a link).

This is what I'm doing


and this is my arraylist class



Can you please tell me what I'm doing wrong and what needs to be replaced?
Thanks
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You're creating an object list then not using it.  Instead you call getsList() in a static way, using the class name instead of the object.  Try:
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Technically, it should be getSList().

Or better yet, getSensorList().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!