1

Question 1:

I have a function that is called every time I get a new Lat Long value, I would like to add these Lat Long values to a list but every time I get a new value the list is over written with the last value (not appended).

My code (with only latitude)

    public void getDistance() {

    List<String> list = new ArrayList<String>();
    list.add(latitudeString);

    System.out.println("list: "+list);  
}

Question 2:

Once I have values in the list (which will increase as values change) I would like to get the last two values everytime the list changes and perform some function (get distance between last two Lat Long values in list and so on..)

1
  • 1
    every time I get a new value the list is over written that is not so weird considering you remake the list every time Commented Jul 31, 2015 at 13:20

5 Answers 5

3

Every time you call

List<String> list = new ArrayList<String>();

a new list will be generated. You need to declare it as a private field in the class (outside the method):

private List<String> list = new ArrayList<String>();

If you want to do calculations though, you need to store them as Doubles, not Strings. That would mean your code will be something like this:

private List<Double> latitudes = new ArrayList<Double>();
private List<Double> longitudes = new ArrayList<Double>();

public void getDistance() {

    // get lat & lon

    Double latitude = Double.parseDouble(latitudeString);
    Double longitude = Double.parseDouble(longitudeString);

    if (latitudes.size() > 0) {
        Double lastLatitude = latitudes.get(latitudes.size() - 1);
        Double lastLongitude = longitudes.get(longitudes.size() - 1);
        // do your calculations
    }

    latitudes.add(latitude);
    longitudes.add(longitude);

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I can get old and new with: // do your calculations System.out.println("latitude NEW: " +latitudeString); System.out.println("Latitude OLD: " +latitudes.get(latitudes.size() -1));
1
List<String> list = new ArrayList<String>();
public void getDistance() {
list.add(latitudeString);
System.out.println("list: "+list);  
}

You should instaniate your list only once in the starting.

Comments

1

Do it like :

List<String> list = new ArrayList<String>();//declare it outside the method
public void getDistance() {
list.add(latitudeString);

System.out.println("list: "+list);  
}

For the second ques:

    List<String> list = new ArrayList<>();
    int size = list.size();
    String[] data = new String[2];
    data[0] = list.get(size - 2);//2nd to last
    data[1] = list.get(size - 1);//last

Comments

0

You will need to declare a class member instead of creating a new local list everytime you call getDistance(). Also, since the list will store latitude and longitude information, I suggested you use double instead of String.

Move the declaration of list to the beginning of the class, and change the type of list and latitudeString to double:

class Class1 {
    List<double> list = new ArrayList<double>();
    ...
    public void getDistance() {
        list.add(latitudeString /*should be a double*/);
        ...
    }
}

Get the last two values using the get() and size() methods:

double value1 = list.get(list.size() - 1); //last value
double value2 = list.get(list.size() - 2); //second-to-last value

Comments

-1

1- You should instantiate your list only once in the starting.

List<String> list = new ArrayList<String>();
public void getDistance() {
list.add(latitudeString);
System.out.println("list: "+list);  
}

2-You will easily get last two items using list.size()-1{your last item} and list.size()-2{Your second last item}.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.