Reputation: 25999
Sorry I suspect the answer is easy but I'm getting annoyed at the way I'm currently doing this and was trying to find another way(old python habits, sorry).
I basically want to create a list of list(list has two integers) so for example it looks like this [[0,0], [0,1],[1,2]]
and so on.
Right now the two ways I have been doing it(which I don't think is right) is the tutorial way of:
list1.add(data);
list1.add(more_data);
final_list_of_list.add(list1);
I did some digging to find another way because I'm lazy and want to do this in one shot, so I found a command that fit my needs(in terms of getting data in)
List final_list_of_list = new ArrayList();
final_list_of_list.add(new Point(0, 0)); //and so on..
The problem with point is when I look at my beautiful data I see the ugliness of this:
[java.awt.Point[x=970,y=10], java.awt.Point[x=65,y=10], java.awt.Point[x=729,y=10]
Surely there is a easier way to add the data and still have it look nice(like the format above in my example)?
Upvotes: 0
Views: 84
Reputation: 2295
There are several things here that don't seem clear:
a) By "looking" at your data you seem to mean printing it with toString(). If you want that to look different, you have to provide your own toString() method. Or use some other way to "look" at your data.
b) You might want to define your own class for pairs, for example as Pair, that way you will be far more flexibile that with Point (which only takes ints, IIRC). Then you should use generics for your lists, and the list you want looks like
List<Pair<type1, type2>> allMyData;
That's not exactly the least amount of typing, but the best way of programming ;-)
Upvotes: 2
Reputation: 845
I believe you can overide the point object's "tostring method." Take a look at this.
Upvotes: 1
Reputation: 9584
You could subclass Point
or create your own implementation and override the toString()
method to print something prettier. I think that would be easier than trying to work with lists of lists.
Perhaps something like this:
public class MyPoint {
public final int x;
public final int y;
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "[" + x + "," + y + "]";
}
}
Upvotes: 3