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.:confused:
Re: How to properly make this ArrayList?
Could you provide a code example of what it is you want to accomplish?
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.
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:
Code :
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.
Code :
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).
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. :P
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:
Code :
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.
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?
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"...}.
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).
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.
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.