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 7545053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:49:49+00:00 2026-05-30T08:49:49+00:00

At present I have an ArrayList<Waypoint> where Waypoint is a simple class that is

  • 0

At present I have an ArrayList<Waypoint> where Waypoint is a simple class that is used to represent a position in an x/y-grid. Now I noticed that for one method I am always using the same instane of a Waypoint object — but I keep changing it’s values.

So if we have say a Waypoint WP which is initialised as X = 0, Y = 0 and I add that to the ArrayList, what exactly is saved? The WP element or just a copy that holds it’s values? Or, asked differently, if I would now increment the values of WP and save it to the ArrayList would the elements at position 0 and 1 in the ArrayList be exactly the same?

If yes, is there an “easy” way to create and save a copy a Waypoint element without having to create a new instance every time?

Thanks in advance!

  • 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-05-30T08:49:50+00:00Added an answer on May 30, 2026 at 8:49 am

    Java works by references so the Waypoint object, if not explicitly cloned, will be the same.

    If you keep a reference to it you can easily change its values while having these reflected into the same object contained in the list, just because they both reference to the same instance.

    This will show the behavior you need:

    static class Pair
    {
        int x,y;
    
        public Pair(int x, int y) { this.x = x; this.y = y; }
    }
    
    public static void main(String[] args)
    {
        java.util.ArrayList<Pair> pairs = new java.util.ArrayList<Pair>();
        Pair p = new Pair(0,0);
        pairs.add(p);
        p.x = 20; p.y = 40;
        System.out.println(pairs.get(0).x+" "+pairs.get(0).y);
    }
    

    You’ll see that output is "20 40" as you’d expect.

    If you want to generate a new waypoint from the one you are starting from then you should define your own method and duplicate explicitly the object.

    Doing

    list.add(p);
    // modify p
    list.add(p);
    

    would just insert the same element twice, which is not what you are looking for. Instead:

     list.add(p);
     Waypoint p2 = p.duplicate();
     list.add(p2);
    

    would insert two different objects (assuming that duplicate actually instantiates a different object with new)

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

Sidebar

Related Questions

At present I have a class that is calling the static method of a
I've noticed that a few Wordpress blogs have query statistics present in their footer
I have two screens on my app that I want to present to users,
so i want to have an arraylist that stores a series of stock quotes.
I have an ArrayList of type String that contain certain values. I want to
I have an ArrayList and want to show items present in the arraylist inside
I have a fairly large chunk of code that produces/returns an arraylist of search
I have an application that has a Service that uses an ArrayList<Double> to store
I have an arraylist of objects that I want to create all possible combinations
I have an arraylist of values that are sorted both numerically and alphabetically .

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.