0

I have the following for loop which looks through a string ArrayList of results, each item in the string is seperated by "::":

ArrayList<String> resultsArray = MyClass.results;
        Integer numPoints = resultsArray.size();

        for (int i =0;i<numPoints;i++){
            String[] pointDetails = resultsArray.get(i).split("::");
            String pointName = pointDetails[0];
            String pointDescription = pointDetails[1];
            String coordinates = pointDetails[2];

        //Turn coordinates into geopoints
            String coord[] = coords.split(",");
            Integer  lng= (int) (Double.valueOf(coord[0]) * 1000000);
            Integer lat = (int)(Double.valueOf(coord[1])*1000000);
            GeoPoint gPoint = new GeoPoint(lng,lat);


         arrayPointName = new ArrayList <String>();
         arrayPointDescription = new ArrayList <String>();
         arrayPointCoords=new ArrayList<GeoPoint>();
         arrayPointName.add(pointName);
         arrayPointDescription.add(pointDescription);
     arrayPointCoords.add(gPoint);
          }

I know I have 20 points in the initial string ArrayList and have printed out its size to check this. However, when I print out the new arraylists, such as arrayPointName, they only contain one point. Any idea on why this is?

3
  • You're creating a new instance of arrayPointName within your for loop each time you iterate, meaning you will only ever have a single item in it. Commented Apr 19, 2013 at 15:01
  • The best way to solve your problem is to trace your code using your IDE's debug tools, it will take you a couple of minutes to locate the problem. Commented Apr 19, 2013 at 15:01
  • Because you are re-creating arrayPointName inside the for loop Commented Apr 19, 2013 at 15:01

2 Answers 2

7

Look at this code:

arrayPointName = new ArrayList <String>();
arrayPointDescription = new ArrayList <String>();
arrayPointCoords=new ArrayList<GeoPoint>();

Those three statements - assigning new, empty ArrayList references to your variables - are being executed on every iteration of your loop.

They should come before your loop instead: you only want to initialize the variables once (creating the three lists) and then add a new item on each iteration.

As a side note, populating multiple collections like this is normally a bad idea. It's usually better to create a single type which encapsulates the related data (name, description, coordinates in this case) and then create a single collection of items of that type. That's usually a lot easier to work with.

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

Comments

0

you used coords as an ArrayList Without initiate it .Also you initiate for each iteration arrayPointName, arrayPointDescription and arrayPointCoords that's why they lost the value created in the previous iteration. they should be initiate juste one time before starting the loop

it will be easy to help you if you give us a sample of resultsArray strring.

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.