Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: How to properly make this ArrayList?

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to properly make this ArrayList?

    I have worked with these a few times but they are still somewhat of a mystery to me. I am currently working on a script in which I need to implement an ArrayList so I can edit the data when I need to. I have only used ArrayLists in the first class to create the constructor of a second class, and initialized each parameter of the arrayList to separate variables in that constructor. This script is different, so there's a catch (or two). It's mixed data type of Strings and Ints. I need to either
    1) create an ArrayList as described above but without initializing the parameters to separate variables (there's a lot of items in the ArrayList) but still use the items in the ArrayList.
    2) create the ArrayList within the 2nd class and initialize it under the constructor (or a method).
    3) Separate the ints and Strings into arrays and combine them into an ArrayList usable by the 2nd class. I know there's a much easier way than the way I'm doing it so that's why I'm requesting help here. That and I can't get any way to work.
    Last edited by helloworld922; March 3rd, 2012 at 07:40 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to properly make this ArrayList?

    Could you provide a code example of what it is you want to accomplish?

  3. #3
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: How to properly make this ArrayList?

    I'm still confused; even after re-reading this. But I can offer this:

    1) ArrayLists -- and all other Collections, for that matter -- don't work with primitive types. Since they require a class parameter (<ClassParameter> goes inside the < >), primitive types are invalid to be stored by them (however, you can use the primitive wrapper, or Number, classes). (This makes more sense if you look at how Collections are set up -- take a look at Oracle's tutorial and the ArrayList documentation).

    2) ArrayLists should hold one and only one type of Object. I say "should" because there are subtleties by which this rule can be skirted upon compilation; however, ultimately, one not following this rule will end up with a runtime error. So, if you need to store Strings and Integers, I suggest using two separate ArrayLists, one for the Strings and another for the Integers.

    Again, I may be directing you incorrectly, as I remain confused about your problem. I would highly appreciate some of your code or a small, contained example code to illustrate what you mean. When posting code remember to use highlight tags:
    [highlight=Java]Your code here[/highlight]




    @helloworld922

    xD Thanks for editing that numbered list.
    Last edited by snowguy13; March 3rd, 2012 at 07:54 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How to properly make this ArrayList?

    Define a class representing the int/String pair. Eg, a planet class might consist of a name and a mass:

    public class Planet {
        private int mass;
        private String name;
     
        public Planet(String name, int mass) {
            this.name = name;
            this.mass = mass;
        }
     
        public String getName() {
            return name;
        }
     
        public int getMass() {
            return mass;
        }
    }

    Now you can create a list of these things, populate it and use it as the argument to constructors, or the argument to or return value from methods.

    public class PlantarySystem {
        private List<Planet> planets;
     
        public PlantarySystem() {
            planets = new ArrayList<Planet>();
        }
     
        public void addPlanet(Planet p) {
            planets.add(p);
        }
     
        public int getNumPlanets() {
            return planets.size();
        }
     
        public List<Planet> getPlanets() {
            return new ArrayList<Planet>(planets);
        }
    }

    Notice how the planet's name and mass can be referred to with descriptive variables and methods. And how adding planets does not involve tiresome (and error prone) bookkeeping to make sure "parallel" arrays are kept in sync. The planet's name and mass are attributes of the same thing so it feels more natural for them to be combined into a structure (a class) rather than have them as elements of separate arrays (or lists for that matter).

  5. The Following 2 Users Say Thank You to pbrockway2 For This Useful Post:

    mwebb (March 3rd, 2012), snowguy13 (March 3rd, 2012)

  6. #5
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: How to properly make this ArrayList?

    @mwebb

    Here's a specific question for you:

    Do the Strings and ints that you are working with come in pairs, or are you instead working with data which contains mingled Strings and ints?

    If the former is true, follow pbrockway's advice (thoroughly); if the latter is true, you may have to use two separate ArrayLists, as I suggested.

    But we still need more information.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  7. #6
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to properly make this ArrayList?

    As of earlier I figured I could make the ints into Strings and cast them back into ints after retrieving them. But I'll clarify because I'm still open to better techniques.

    I have 52 items that needs to be stored in some sort of array. Hoping for arrayList, not sure where to put it, or how to type it out correctly.

    Example:
    Class 1:
    //main
        ...//(ArrayList declared here?/how?)
     
     
    Class2:
    //private variables declared (ArrayList declared here?/how?).
    //constructor.
        //private variables initialized (ArrayList declared here?/how?).
    //end of constructor.
     
    //some method that needs data picked from that ArrayList. 
    //some method.
    //some method.
    //some method.

    Sorry for being so broad, I'm not sure how else to best explain it.
    Last edited by mwebb; March 3rd, 2012 at 08:25 PM.

  8. #7
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: How to properly make this ArrayList?

    ...Okay, I'm a little more confused now. That casting idea is a good one; however, you would need a way to check if something was originally an int (I'd use try and catch with Integer.parseInt()). There is a problem I foresee here, though -- what if a piece of data was originally a String, but contained only digits? It could be successfully parsed into an int, despite it being originally a String.

    Could you please answer my previous question:

    Do the Strings and ints that you are working with come in pairs (one String and one int), or are you instead working with data which contains mingled Strings and ints?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  9. #8
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to properly make this ArrayList?

    They contain mingled Strings and ints as in {1,2,3,4,5,6,7,"Food","Donut","Cow","STD"...}.

  10. #9
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: How to properly make this ArrayList?

    Okay. In that case, I'd use two ArrayLists, one of Strings and another of Integers (the Integer class I mean; ints won't work).
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  11. #10
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to properly make this ArrayList?

    But how would I go about retrieving say a random object from both ArrayLists as if they were one? That's kind of why I was trying to see if I can co-mingle the data.

  12. #11
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: How to properly make this ArrayList?

    Hm...

    Then I would try using the casting as you had suggested earlier. This wouldn't run into any problems, so long as you don't run into any all-digit strings (such as "693" or "212"); these Strings would appear exactly as ints converted to Strings. But don't let that scare you from trying it. I'd give it a go.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. not looping properly, please help.
    By nakedtriple in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 28th, 2011, 08:31 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM