-3

The expected output of my index 0 Array list is the name and the number. But its showing weird result why?

fullcode here enter image description here

Metaa
  • 3
  • 3
  • 1
    Please don't post code as images. Copy the relevant parts of the code and paste them into the question. – markspace May 20 '22 at 15:57
  • Read [why you should not upload images of code](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question). – Chaosfire May 20 '22 at 15:58
  • 1
    Yes, always explain what you are seeing (copy paste again) and explain what you wanted to see instead. In your case it looks like you didn't override the `toString()` method. https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/Object.html#toString() – markspace May 20 '22 at 15:59
  • Thank you everyone the toString is the answer! – Metaa May 20 '22 at 16:16

1 Answers1

0

Don't post images. Post the code. If you want to print arrays, then you should use Arrays.toString method. Every element in the array should also have a toString method. Here is an example:

class Student{
    int id;
    String name;
    public Student(int id, String name){
        this.id = id;
        this.name = name;
    }
    @override
    public String toString(){
        return "Student " + name + ", with id: " + id;
    }
}

Then use something like this in the main method:

ArrayList<Student> studentList = new ArrayList<>();
studentList.add(new Student(134, "Jake"));
studentList.add(new Student(435, "Mark"));

System.out.println(Arrays.toString(studentList));
Jhanzaib Humayun
  • 1,193
  • 1
  • 4
  • 10