Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:39:11+00:00 2026-06-11T16:39:11+00:00

So, this is my class: public class SegmentMaker { public ArrayList<Segment> makeSegments(CustomNodes cn, Trip

  • 0

So, this is my class:

public class SegmentMaker {

    public ArrayList<Segment> makeSegments(CustomNodes cn, Trip myTrip) {

        ArrayList<Segment> segments = new ArrayList<Segment>();
        ArrayList<Integer>segmentNodes = new ArrayList<Integer>();

        int count = 0; 

        for (int i=0; i < myTrip.getTripList().size(); i++){
            Log.d(TAG, "checking " + cn.getOsmID(myTrip.getTripItem(i)));

            if (i==0) {
                segmentNodes.add(myTrip.getTripItem(i));     
            }

            else if (i==myTrip.getTripList().size()-2) {
                segmentNodes.add(myTrip.getTripItem(i));     
            }

            else if (i==myTrip.getTripList().size()-1) {
                segmentNodes.add(myTrip.getTripItem(i)); 
                Segment segment = new Segment();
                segment.setId(count); 
                segment.setNodeIds(segmentNodes); 

                Log.d(TAG, "in SM segment " + segment.getId() + " nodes: " + segment.getNodeIds().toString());
                segmentNodes.clear(); 
                count++; 
                segments.add(segment);
                Log.d(TAG, "in SM segment " + segment.getId() + " nodes: " );
                for (int x=0; x<segment.getNodeIds().size(); x++) {
                    Log.d(TAG, "   " + segment.getNodeIds().get(x));
                }
                break; 
            }

            else if ((getAngle(myTrip.getTripItem(i), myTrip.getTripItem(i+1), myTrip.getTripItem(i+2),cn) < 25)  &&  
                    (getAngle(myTrip.getTripItem(i), myTrip.getTripItem(i+1), myTrip.getTripItem(i+2),cn) > -25))  
            {
                segmentNodes.add(myTrip.getTripItem(i));         
            }

            else {
                segmentNodes.add(myTrip.getTripItem(i)); 
                Segment segment = new Segment();
                segment.setId(count); 
                segment.setNodeIds(segmentNodes); 

                Log.d(ONE, "in SM segment " + segment.getId() + " nodes: " + segment.getNodeIds().toString());

                segments.add(segment);
                segmentNodes.clear(); 
                count++; 
            }
        }

        Log.d(TWO, " segments SM ende: " + segments.get(0).getNodeIds().size());
        return segments; 
    }

    public float getAngle(int firstItem, int secondItem, int thirdItem, CustomNodes cn){
        //do some stuff

        return angle;
    }
}

I’m sorry, it’s much to read, what I’m trying to do is: a Segment has an ID and an ArrayList of NodeIDs. I collect all the NodeIDs until one doesnt pass the getAngle- check, then go to the next segment.

Where I Log.d with the ONE tag, all the NodeIDs are in the segment. Where I Log.d with the TWO tag, the ArrayList of NodeIDs is empty. When I return the segments- ArrayList, the segments in it are there (havin an ID), but the NodeID- ArrayLists of all the segments are empty. Where am I losing the content? I’m going crazy over this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:39:12+00:00Added an answer on June 11, 2026 at 4:39 pm

    Your issue stems from a misunderstanding of how Java handles Objects and references.

    segmentNodes.add(myTrip.getTripItem(i)); 
    Segment segment = new Segment();
    segment.setId(count); 
    segment.setNodeIds(segmentNodes); 
    
    Log.d(TAG, "in SM segment " + segment.getId() + " nodes: " 
        + segment.getNodeIds().toString());
    segmentNodes.clear(); 
    count++; 
    segments.add(segment);
    

    Let’s take a look at what Java is going to your objects as you “store” them. In the first chunk, you added a “trip” to segmentNodes. Then you added a reference to segmentNodes to the newly created Segment object. Because you added a reference, all changes made to segmentNodes will also be seen within the Segment‘s reference of segmentNodes.

    Then you printed some data out, but you aren’t altering anything so nothing should change.

    Then, you call segmentNodes.clear(). This cleared out all the data in segmentNodes, but since you gave a reference of segmentNodes to the segment instance, it lost all the integers, too. Now, if you want the segment object to have its own copy of the segmentNodes list so that changes to the segmentNodes list does not affect the list in the Segment instance.

    To do that, manually create a copy:

    ArrayList<Integer> segCopy = new ArrayList<Integer>();
    for(Integer i : segmentNodes){
        segCopy.add(i);
    }
    segment.setNodeIds(segCopy);
    segmentNodes.clear();
    

    Java does not pass copies of Objects, it passes a copy of the reference. You may find this Stackoverflow Question about this topic helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have this class: public class Account { public int AccountID { get;
I have this class public class wordObject implements java.io.Serializable { String wordName; int occCount;
I have this class public class Subject { int ID; string Name; int Semester;
I have this class: public class MyEntity { public virtual int Id { get;
Suppose you have this class: public class A { private int number; public setNumber(int
I've got this class: public class A<T> { protected T something = new T();
I have this class: public class Fibonacci { public static int Calculate( int x
I have this class: public class docInfo { private int freq; private HashMap<String, Double>
I have this class: public class TestClass { public TestClass(int? foo, string bar) {
I have this class: public abstract class Directory { protected int id; protected File

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.