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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:50:06+00:00 2026-06-15T04:50:06+00:00

While coding in Android, I needed an ArrayList of Points which I called wormPt.

  • 0

While coding in Android, I needed an ArrayList of Points which I called wormPt. I initialized it through a loop.

ArrayList<Point> wormPt = new ArrayList<Point>();
Point pt = new Point();
.
.
.
private void initializeWorm() {
    // TODO Auto-generated method stub
    pt.x = 220;
    pt.y = 300;
    for (int i = 0; i <= 5; i++) {
        wormPt.add(pt);
        Log.d("wormdebug", wormPt.toString());

        pt.x -= 5;
    }
    Log.d("wormdebug", wormPt.toString());
}

My last log.d should report points
(220,300)
(215,300)
(210,300)
(205,300)
(200,300)
(195,300)

Instead, all my points are (190, 300)

Here is my log data

11-21 23:48:11.549: D/wormdebug(3273): [Point(220, 300)]
11-21 23:48:11.600: D/wormdebug(3273): [Point(215, 300), Point(215, 300)]
11-21 23:48:11.600: D/wormdebug(3273): [Point(210, 300), Point(210, 300), Point(210, 300)]
11-21 23:48:11.600: D/wormdebug(3273): [Point(205, 300), Point(205, 300), Point(205, 300), Point(205, 300)]
11-21 23:48:11.600: D/wormdebug(3273): [Point(200, 300), Point(200, 300), Point(200, 300), Point(200, 300), Point(200, 300)]
11-21 23:48:11.600: D/wormdebug(3273): [Point(195, 300), Point(195, 300), Point(195, 300), Point(195, 300), Point(195, 300), Point(195, 300)]
11-21 23:48:11.630: D/wormdebug(3273): [Point(190, 300), Point(190, 300), Point(190, 300), Point(190, 300), Point(190, 300), Point(190, 300)]
11-21 23:48:14.669: W/KeyCharacterMap(3273): No keyboard for id 0
11-21 23:48:14.679: W/KeyCharacterMap(3273): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

I tried
Can't add element to ArrayList in for loop and others, but they don’t seem to be having the same problem. Any help will be appreciated.
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-06-15T04:50:07+00:00Added an answer on June 15, 2026 at 4:50 am

    The problem is that your ArrayList contains several references to the same object. All you’re doing in the loop is adding the same reference and mutating the object.

    If you change the loop to create a new Point on each iteration, it will work:

    int x = 220;
    for (int i = 0; i <= 5; i++) {
        wormPt.add(new Point(x, 300));
        x -= 5;
    }
    

    It’s very important to understand the difference between variables, objects and references. pt is a variable. Its value is a reference to a Point object. Unless you ask for a new object, Java won’t create one for you. For example:

    Point a = new Point(10, 20);
    Point b = a; // Copies the *reference*
    a.x = 100;
    System.out.println(b.x); // 100
    

    Note that this isn’t associating the a and b variables with each other – it’s just giving them the same value (the same reference). So you could later change a to be a reference to a different Point, and that wouldn’t change b:

    Point a = new Point(10, 20);
    Point b = a; // Copies the *reference*
    a.x = 100;
    a = new Point(0, 0); // This doesn't affect b, or the object its value refers to
    System.out.println(b.x); // 100
    

    In this case, it’s a bit like giving 10 different people a piece of paper with your home address on. If one of those people visits the address and paints the front door green, then another of them visits the address, they’ll see a green front door.

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

Sidebar

Related Questions

I Want to know which one is preferred while coding to use Static Methods
I used many different browsers while I was coding for a new site (Firefox,
Back in my C/C++ days, coding an infinite loop as while (true) felt more
I am coding up an Android app and sometimes exceptions occur. So while I
Quite new to coding for android but this issue has me tearing my hair
im new to android gaming and started andengine and facing problem while using createTiledFromAsset
While coding python I'm using only 2 spaces to indent, sure PEP-8 really recommend
Similar Questions While coding, how many columns do you format for? What is a
I've been drooling over Django all day while coding up an internal website in
I have a system for users to apply for driving permit. However, while coding

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.