Joe O'Brien
Joe O'Brien

Reputation: 47

Why is there a list object within my list?

I'm creating an list that stores the latlng positions of images stored in my phone, but when I check the output I notice there are two square brackets at each end.

output

here is the code:

protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo);

    LatLngs = (TextView)findViewById(R.id.LatLngs);


    List imagePaths = getCameraImages(this);

    List realLatLngs = getLatLong(imagePaths);

    LatLngs.setText(Arrays.toString(new List[]{realLatLngs}));

}

public List<String> getLatLong(List<String> paths) {

    List<String> trueLatLongList = new ArrayList<>();
    for(int i = 0; i< paths.size(); i++){
        String imagePath = paths.get(i);

        String trueLat = ReturnLat(imagePath);
        String trueLng = ReturnLng(imagePath);

        String trueLatLng = "(" + trueLat + ", " + trueLng + ")";


        trueLatLongList.add(i, trueLatLng);
    }
    return trueLatLongList;
}

What is wrong that is causing this? Is it the new List I create in the getLatLong() method?

Upvotes: 2

Views: 65

Answers (2)

OldCurmudgeon
OldCurmudgeon

Reputation: 65831

A guess:

LatLngs.setText(Arrays.toString(new List[]{realLatLngs}));

This is creating a new List[] containing just one element (the List realLatLngs) and then calling Arrays.toString on it. You are therefore stringifying an array of lists which would correctly result in [[...]].

Try

LatLngs.setText(realLatLngs.toString());

Upvotes: 1

Benkerroum Mohamed
Benkerroum Mohamed

Reputation: 1936

That's because you are pushing your list inside an other one, replace LatLngs.setText(Arrays.toString(new List[]{realLatLngs})); with LatLngs.setText(Arrays.toString(realLatLngs));

Upvotes: 0

Related Questions