Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I want my toString method to print out each item of the arrayList<Integer> w

ID: 3542101 • Letter: H

Question

Hi, I want my toString method to print out each item of the arrayList<Integer> with a for loop but I dont seem to make the right approach. This is what I got:


public String toString(){

String n = "";

for(int i = 0; i<bigNumber.size(); i++){   

n = ""+bigNumber.get(i).toString();

}

return n;

}


the bigNumber is a private ArrayList<Integer>, that has been already assigned a value.


I keep getting a wird adress instead of what I want and I know its that way because the value that is assigned during the for loop are local to that forloop or something like that. But this is what most made sense to me. Please help!

Explanation / Answer

public String toString()
{
String n = "";
for(int i = 0; i<bigNumber.size(); i++)
{
// here you have to append all to n
// but every time you are not appending...
// update code below and run your program.
n = n+bigNumber.get(i).toString();
}
return n;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote