user5174952
user5174952

Reputation: 147

Add String Variable To List (Keep Adding To List)

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..)

Upvotes: 1

Views: 2321

Answers (5)

deezy
deezy

Reputation: 1480

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

Upvotes: 0

Nikhil Verma
Nikhil Verma

Reputation: 1730

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

Upvotes: 1

Adarsh Yadav
Adarsh Yadav

Reputation: 3770

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}.

Upvotes: -1

Nishant
Nishant

Reputation: 575

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.

Upvotes: 1

Glorfindel
Glorfindel

Reputation: 22631

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);

}

Upvotes: 3

Related Questions