I'm learning Java and am trying to wrap my head around these data structures. I'm coming from Python/PHP so I'm used to dynamically sizing arrays on the fly and being able to store multiple data types in one array.
How would I best store this data? Say I don't know how many rows I'll have, but I do know that each row will hold 2 columns of data. One column being a string, and the other column being a double.
Example in pseudo-ish code if I had 3 rows:
array(array("description1", 10.00),
array("description2", 12.00),
array("description3", 14.00));
Then, I want to loop through the array to process the datawith something like:
foreach(rows as row){
myStringVal = row[0]; //on first iteration would be "description1"
myIntVal = row[1]; //on first iteration would be 10.00
... do something with the values ...
}
I'm thinking I need to create an arraylist that holds an array, but I can't store both strings and doubles in a java array, so what do i do? Do I use a map instead, and treat it as if it were an array? For example, do I create a map where the first element is a numeric ID for each row, the second element is the string value, the 3rd element is the double value, and then I use a loop to increase a counter and grab each row from the map that using the numeric ID>
Really confused as to how this will work. Any suggestions? Thanks!
You're not storing "different kind of values", you're storing semantic data that encodes "a tuple" but using the non-java concept of just sticking that in an array. Don't do that, instead use the C/C++/Java concept of encoding linked data as a struct/object:
// our "struct"esque class for the linked data we're handling
class MyObject {
String description;
float value;
Public MyObject(String description, float value) {
this.description = description;
this.value = value;
}
}
// build our list of objects
ArrayList<MyObject> mylist = new ArrayList<MyObject>();
mylist.add(new MyObject("description1", 10));
mylist.add(new MyObject("description2", 12));
mylist.add(new MyObject("description3", 14));
...
// and then we iterate
String d; float v;
for(MyObject m: mylist) {
d = m.description;
v = m.value;
...
}
It looks like a List<Map<String, BigDecimal>>
to me.
List
will give you the ability to enter more than one element continuously. The Map
will give you the association between the String
and floating-point value, which I've chosen BigDecimal
to represent. You could use Double
if you wanted.
List<Map<String, BigDecimal>> elements = new ArrayList<>(); elements.add(new HashMap<String, BigDecimal>()); elements.get(0).put("description1", BigDecimal.valueOf("10.00"));
Use a class instead of an array.
public class X
{
private final String description;
private final double value;
public X(final String desc,
final double val)
{
decription = desc;
value = val;
}
// getters
}
If you want to be able to change the description and value then don't make them final.
You could make the description
and value
variables public, but I would avoid that temptation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.