#Hey! Im doing a school assignment and I want to add an object to an array.

1 messages ยท Page 1 of 1 (latest)

twin mango
#

The object already exists in another array but i need to add it to another in case there are multiple instances which need to go into the new array.

                public void findOldest() {
        int oldest = 0;
        Person[] oldestPeople = new Person[5];
        for(int i = 0; i < groupie.length; i++)
        {
            if(groupie[i].getAge() >= oldest) {
                System.out.println(oldest);
                oldest = groupie[i].getAge();
            }
        }
        
        for(int i = 0; i < groupie.length; i++)
        {
            if(groupie[i].getAge() == oldest) {
                                //error below
                oldestPeople.add(groupie[i]);
            }
        }
    }
candid chasmBOT
#

<@&987246399047479336> please have a look, thanks.

candid chasmBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

be stored separately. How can I do that?

analog epoch
#
private Person[] groupie = new Person[5];
                public void findOldest() {
        int oldest = 0;
        Person[] oldestPeople = new Person[5];
        for(int i = 0; i < groupie.length; i++)
        {
            if(groupie[i].getAge() >= oldest) {
                System.out.println(oldest);
                oldest = groupie[i].getAge();
            }
        }

        for(int i = 0; i < groupie.length; i++)
        {
            if(groupie[i].getAge() == oldest) {
                                //error below
                oldestPeople.add(groupie[i]);
            }
        }
    }
candid chasmBOT
# analog epoch ```java private Person[] groupie = new Person[5]; public void fi...

Detected code, here are some useful tools:

Formatted code
private Person[] groupie = new Person[5] ;
public void findOldest() {
  int oldest = 0;
  Person[] oldestPeople = new Person[5] ;
  for (int i = 0; i < groupie.length; i++) {
    if (groupie[i] .getAge() >= oldest) {
      System.out.println(oldest);
      oldest = groupie[i] .getAge();
    }
  }
  for (int i = 0; i < groupie.length; i++) {
    if (groupie[i] .getAge() == oldest) {
      //error below
      oldestPeople.add(groupie[i] );
    }
  }
}
analog epoch
#

alright, now its readable ๐Ÿ™‚

#

u cant .add to an array

#

arrays are fixed size structures

#

they cant change their size ever

#

u can only set one of the existing spots

#

array[index] = ...

#

if u want a structure that can grow and shrink, use Lists

honest cove
#

Add the new item and convert it back to array

twin mango
#

ooooh you are right

#

im gonna do it with the index, my teacher told me we are gonna cover arraylists next week so ill save that for then

#

thank you guys:)

honest cove
#

๐Ÿ‘