Reputation: 13
I have 2 lists that I send to MySql.
public ArrayList<Double> lat = new ArrayList<Double>();
public int l = lat.size();
public ArrayList<Double> lng = new ArrayList<Double>();
public int ll = lng.size();
Double l = lat.get(i);
Double ll = lng.get(i);
String sql = "INSERT INTO tab (lat, lng) VALUES('" + l + "', '" + ll + "')";
stmt.executeUpdate(sql);
but in the wrong order, so I put the
Collections.reverse(lat);
Collections.reverse(lng);
before
Double l = lat.get(i);
Double ll = lng.get(i);
and everything works in good order, but when I use this method again with this same elements in list the order will be reversed again ...
Adding elements to the list:
lat.add(l, currentLatitude);
lng.add(ll, currentLongitude);
How to do, that elements were in good order all the time?
Upvotes: 2
Views: 125
Reputation: 338266
Call List#reversed
for a reversed-encounter-order view onto the original list, without changing the original.
Double l = lat.reversed().get(i); // A different encounter order, but *not* changing the list’s original encounter order.
Double ll = lng.reversed().get(i);
SequencedCollection#reversed
As of Java 21+, the List
interface is a sub-interface of the new SequencedCollection
interface. See JEP 431: Sequenced Collections for details.
As an implementation of List
& SequencedCollection
, ArrayList
now offers the reversed
method. This method returns a List
object which is really a view onto the original list. So calling List#reversed
does not alter the original list’s sequence (encounter order), in contrast to Collections.reverse
which does alter the original.
List < Double > oneTwoThree = List.of( 1d , 2d , 3d );
List < Double > threeTwoOne = oneTwoThree.reversed( );
System.out.println( "oneTwoThree = " + oneTwoThree );
System.out.println( "threeTwoOne = " + threeTwoOne );
System.out.println( "oneTwoThree = " + oneTwoThree );
When run:
oneTwoThree = [1.0, 2.0, 3.0]
threeTwoOne = [3.0, 2.0, 1.0]
oneTwoThree = [1.0, 2.0, 3.0]
Upvotes: 0
Reputation: 1938
Collections.reverse
method updates the lat
and lng
order and it is not ok for you because this code is performed by a button and it is executed all the time. I think you could just iterate the lat
and lng
arraylists starting from the end:
public ArrayList<Double> lat = new ArrayList<Double>();
for(int l=0 ...){
... retrieve currentLatitude
lat.add(l, currentLatitude);
}
public int l = lat.size();
public ArrayList<Double> lng = new ArrayList<Double>();
for(int ll=0 ...){
... retrieve currentLongitude
lng.add(ll, currentLongitude);
}
public int llsize = lng.size();
for(int i=llsize-1;i>=0;i--){
Double l = lat.get(i);
Double ll = lng.get(i);
String sql = "INSERT INTO tab (lat, lng) VALUES('" + l + "', '" + ll + "')";
stmt.executeUpdate(sql);
}
...
Upvotes: 0
Reputation: 16
lat.add(l, currentLatitude);
lng.add(ll, currentLongitude);
Looks a bit weird since according to javadoc (https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-int-E-) l and ll should be indexes and here, you are using double values.
You might have switched the arguments here
Upvotes: 0
Reputation: 2128
Put your lists in a buffer, one for each list.
var bufLat = new ArrayList<Double>(lat);
var bugLng = new ArrayList<Double>(lng);
then every next operation, make it on the buffers:
Collections.reverse(bufLat);
Collections.reverse(bufLng);
Double l = bufLat.get(i);
Double ll = bufLng.get(i);
In this way you avoid to modify the original order of your lists and you can continue to add new items preserving their order of inserting in the lists.
As far as concern your way to do db sql insert into the db:
String sql = "INSERT INTO tab (lat, lng) VALUES('" + l + "', '" + ll + "')";
stmt.executeUpdate(sql);
it is a not very good practice, because it opens to possible sql injection attacks.. for a better solution see this example on SO https://stackoverflow.com/a/8218932/3762855
Upvotes: 0
Reputation: 1129
Use ListIterator and its hasPrevious API. It will help you parse list in reverse order.
public ArrayList<Double> lat = new ArrayList<Double>();
public int l = lat.size();
public ArrayList<Double> lng = new ArrayList<Double>();
public int ll = lng.size();
ListIterator latIt = lat.listIterator(l);
ListIterator longIt = lng.listIterator(ll);
while(latIt.hasPrevious()) {
System.out.println(latIt.previous());
}
while(longIt.hasPrevious()) {
System.out.println(longIt.previous());
}
Upvotes: 1