Results 1 to 14 of 14
  1. #1
    HEIMDAL13 is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default How to add multiple objects/atributes into an array?

    Hi, I have a class that has lots of objects created as local variables (they could be attributes if needed). About 200 hundred or more. (Yes I know thatís a lot, but Initially i didnít have plans to have more than 20...)

    The problem is that now I want to create an ArrayList with all of them, I could just start adding one by one with the method .add but that will be a lot of work. Is there a way to get all the variables/attributes in one array automatically? How can I create such an array list without adding one by one separately?

    Thanks.

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: How to add multiple objects/atributes into an array?

    Are the Variables all of the same type?

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,452
    Rep Power
    25

    Default Re: How to add multiple objects/atributes into an array?

    Is there a way to get all the variables/attributes in one array automatically?
    The first place to look would be the API doc for the ArrayList class to see what methods it has that might do what you want.
    The code needs to tell the ArrayList what elements are to be added and in what order. How can the program specify that?
    Another requirement (as zFollette suggests) is that all the objects be the same type.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    HEIMDAL13 is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default Re: How to add multiple objects/atributes into an array?

    Yes, the variables that I want to get to the list are the same type. But the class have some other variables like the list itself, some counters and other objects.

  5. #5
    HEIMDAL13 is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default Re: How to add multiple objects/atributes into an array?

    I looked at the api, but I couldn't found any method to do that. I tried to search for a class method to get a collection or whatever and then transfere to my array list.

    If I could get some type of List with All the variables in the class, then maybe I would be able to eliminate the ones i don't need and are a diferent type.
    Last edited by HEIMDAL13; 12-24-2013 at 04:18 PM.

  6. #6
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: How to add multiple objects/atributes into an array?

    I was going to say if you really wanted to, you could use a loop to add 200+ of the same type of variable to an ArrayList, like some rat nest of variables. (Do not do this)

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,452
    Rep Power
    25

    Default Re: How to add multiple objects/atributes into an array?

    I tried to search for a class method to get a collection or whatever and then transfere to my array list.
    That's very close to what you need to do.
    There is a method and constructor in the ArrayList class that will use the contents of a Collection. Look at the Arrays class methods to see if there is a way to put the contents of an array into a Collection. (Hint: List implements Collection)

    with All the variables in the class
    There is no way to do that easily.
    Last edited by Norm; 12-24-2013 at 04:28 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    HEIMDAL13 is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default Re: How to add multiple objects/atributes into an array?

    Well, it seems I will have to add them manually one by one... I found something called Fields but I don't know how it works and if it will do the job.

    Anyways thanks for answering

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,452
    Rep Power
    25

    Default Re: How to add multiple objects/atributes into an array?

    Is that the Field class in the reflect package? That should be an interesting exercise.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    HEIMDAL13 is offline Member
    Join Date
    Dec 2013
    Posts
    5
    Rep Power
    0

    Default Re: How to add multiple objects/atributes into an array?

    Sorry, I don't know what a reflect package or Field class is. I was searching my problem and I found that someone was using something related to fields in order to get all the variables of a class in to some kind of container. I tought that would be a simple method or trick to do what i want.

    Is there a way the Reflect package/ Field class can help my problem?

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,452
    Rep Power
    25

    Default Re: How to add multiple objects/atributes into an array?

    the Reflect package/ Field class
    Look in the API doc for that package and its classes: Java Platform SE 7
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,533
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to add multiple objects/atributes into an array?

    What about a Map<String, T> where the key is the name of the variable and the value is the value of the variable of type T.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: How to add multiple objects/atributes into an array?

    Quote Originally Posted by JosAH View Post
    What about a Map<String, T> where the key is the name of the variable and the value is the value of the variable of type T.

    kind regards,

    Jos
    That brings him back to adding them manually, and if he has to do that, there is really no point in that at all.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,533
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to add multiple objects/atributes into an array?

    Quote Originally Posted by zFollette View Post
    That brings him back to adding them manually, and if he has to do that, there is really no point in that at all.
    You don't understand; there is not much difference in writing:

    Java Code:
    String myString;
    and

    Java Code:
    varMap.put("myString", null);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Multiple object instances in array of objects
    By Drizzt in forum New To Java
    Replies: 6
    Last Post: 01-12-2012, 04:07 PM
  2. Multiple Graphics Objects?
    By MrFish in forum Java 2D
    Replies: 7
    Last Post: 10-29-2010, 07:37 PM
  3. Reading file atributes in java??
    By tfitz666 in forum New To Java
    Replies: 5
    Last Post: 03-24-2010, 09:12 PM
  4. Replies: 0
    Last Post: 07-15-2009, 01:46 PM
  5. Can I store multiple objects in an array
    By lareauk in forum New To Java
    Replies: 9
    Last Post: 05-29-2008, 03:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •