1

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?

jacks
  • 4,614
  • 24
  • 34
lukash
  • 3
  • 4

4 Answers4

1

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());
    }
Vinodh
  • 1,069
  • 2
  • 10
  • 22
0

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);
}
...
user6904265
  • 1,938
  • 1
  • 16
  • 21
  • It does not work because "Collections.reverse" is still in the button code and pressing any cause another reversal, and when I put 'Collections.reverse' outside the code button, it will never perform – lukash Dec 06 '16 at 10:15
  • ok, I didn't know about that, "Collections.reverse" does not fit in this case. Try to iterate the array lists starting from the end. I update the code in the answer. – user6904265 Dec 06 '16 at 10:23
  • It looks like it will work, but I do not know how to code this loop for with int l/ll = 0 – lukash Dec 06 '16 at 14:14
0

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

Community
  • 1
  • 1
Ciro Corvino
  • 2,038
  • 5
  • 20
  • 33
  • I would only warned about SQL Injection here because I don't really believe you will be able to inject anything with Double instance. – AxelH Dec 06 '16 at 10:25
  • it is possible instead.. just it needs that you look better at the single quotes around the "l" and "ll" values in the sql statement... – Ciro Corvino Dec 06 '16 at 10:32
  • Could you add an example of how you would inject a query with this ? Yes the query will have character type but in Java, you only have numeric value so I don't see how you could, but I might miss something. – AxelH Dec 06 '16 at 10:33
  • 1
    oops I thought it was C#.. however it is a good practice avoid to call sql statement in that way while it should have always to parameterize sqlStatement specially when they have filters on text fields – Ciro Corvino Dec 06 '16 at 10:50
  • 1
    I agree ! But this can be optionnal sometime (but with caution) – AxelH Dec 06 '16 at 10:54
0
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