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

  • Home
  • SEARCH
  • 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 9010795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:33:28+00:00 2026-06-16T02:33:28+00:00

EDIT: I am rewriting this question, to hopefully make the code more explicit. I

  • 0

EDIT: I am rewriting this question, to hopefully make the code more explicit.

I have a class, Pred, with 2 variables.

public class Pred {

    private String str;
    private ArrayList<Keys> keysArr;

    public Pred(String s) {
        this.str = s;
        keysArr = new ArrayList<Keys>;
    }

    public void setKeysArr(ArrayList<Keys> kArr) {
        this.keysArr.addAll(kArr);
    }
}

In the extract() method, I loop through another array, get some variables, and use these to make new Preds.

public static Collection<Pred> extractPause(Keys[] kseArr) {

    Collection<Pred> predArray = new ArrayList<Pred>(); 

    // create method variables
    ArrayList<Keys> keysArray ka = new ArrayList<Keys>();
    String s = "";

    // loop through an array
    for (int i; i < someArray.size(); i++) {
        s = "Sam"+i;
        ka.add(key1+i);
        ka.add(key2+i);

        // create new instance of Pred
        Pred p = new Pred(s);
        p.setKeysArr(ka);
        predArray.add(p);

        // reset variables (not sure if that's necessary)
        s = "";
        ka.clear();

    }
    return predArray;
}

When I go to print the array of Preds, I run into a weird situation, where I’m getting the array values of only the last Pred in the predArray.

public void print() {
    for (Pred p : predArry)
        System.out.println(p.str+" "+p.get(p.keysArr.size()-1).getKeys());
}

Output:

Sam1 key12 key2  
Sam2 key12 key22  

Should be:

Sam1 key11 key21  
Sam2 key12 key22  
  • 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-16T02:33:29+00:00Added an answer on June 16, 2026 at 2:33 am

    Probably because when you create your list of Foo instances, you do

    Foo foo = new Foo();
    for (int i = 0; i < 10; i++) {
        foo.setBarStr("hello " + i);
        foo.setKeyArray(...);
        list.add(foo);
    }
    

    instead of doing

    for (int i = 0; i < 10; i++) {
        Foo foo = new Foo();
        foo.setBarStr("hello " + i);
        foo.setKeyArray(...);
        list.add(foo);
    }
    

    Remember that a list contains references to objects, and not copies of objects. So if you add the same Foo instance 10 times to the list, the list will contain 10 references to this unique instance, which will of course contain its last modified state.

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

Sidebar

Related Questions

EDIT, I am rewriting the question for more clarity. I have a profile model
I'm currently rewriting some python code to make it more efficient and I have
Edit: I'm rewriting this question because I apparently wasn't clear. Sometimes the GPS service
I have a text file that I want to edit by rewriting it to
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
Edit (updated question) I have a simple C program: // it is not important
EDIT: Simple version of the question: I want to create server variables in the
I am always rewriting my headers and footers and for every edit i have
Code In question I have heard (and regurgitated) the C++ exception mantra on both
EDIT: the IIRF.ini code was not the problem in this case, the performance issues

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.