0

I am new to java and try to use ArrayList but I get random output when running no error I got but random output I got when trying to access ar.grt(0) value .can anyone explain it why I got this type of output

import java.util.*;
import java.util.Comparator;
import java.lang.*;
import java.io.*;


class Student
{

     String name="";
     int roll;
     int marks;
     public Student(String na,int ro,int ma)
     {
        name=na;
        roll=ro;
        marks=ma;   

     }

}

public class CustomSortingClass
{
    public static void main(String []args)
    {   Student s1=new Student("Priyanka",05,75);
        Student s2=new Student("Abhishek",01,90);
        Student s3=new Student("Shivendra",04,80);
        Student s4=new Student("Ritesh",02,95);
        Student s5=new Student("Sonali",06,65);



        ArrayList<Student> ar=new ArrayList<Student>();
        ar.add(s1);
        ar.add(s2);
        ar.add(s3);
        ar.add(s4);
        ar.add(s5);

        System.out.println("unsorted Student\n"+ar.get(0));

    }

}

output is :

F:\java>java CustomSortingClass

unsorted Student

Student@b065c63**
Dhawal Kapil
  • 2,584
  • 18
  • 31
  • Each element added added to your ArrayList **ar** contains an instance of a Student object as outlined by the Student class. Each Student object instance consists of a **Name**, **Roll**, and **Marks** again, as outlined by your Student class via class member variables. To properly view the contents of the Student object instance contained within any ArrayList element you might do something like this: `System.out.println("unsorted Student\n" + "Name: " + ar.get(2).name + "\nRoll: " + ar.get(2).roll + "\nMarks: " + ar.get(2).marks);`. – DevilsHnd - 退職した Aug 13 '19 at 05:09

2 Answers2

1

The random value that you are getting is the hashCode of the object since you are printing the whole object, but you have not defined what to print.

Therefore, the default implementation, when an Object is printed is

getClass().getName() + "@" + Integer.toHexString(hashCode())

which is what you would be getting exactly.

Dhawal Kapil
  • 2,584
  • 18
  • 31
  • can u correct my code .. – Abhishek Bais Aug 13 '19 at 04:49
  • your code is fine :) it's not incorrect. If you want better output, you will need to define the toString for your object. Please read more on https://stackoverflow.com/questions/13498179/output-of-system-out-printlnobject – Dhawal Kapil Aug 13 '19 at 04:50
  • how can i print value of s1 object of arraylist ... – Abhishek Bais Aug 13 '19 at 04:53
  • @AbhishekBais `s1` is an object, you are already getting the standard representation of it. You must first define what you want printed, its name? its roll? its mark? – user85421 Aug 13 '19 at 05:13
  • thanks guys now understood ...confusion arise due to websites which print object directly. – Abhishek Bais Aug 13 '19 at 05:17
  • This is missing the most important point, this is the implemntation of `Object.toString` and can be overridden to printe whatever we want for an instance.. – AxelH Aug 13 '19 at 05:55
-1
ar.add(s1)

should instead be

ar.add(s1.name)
ab123
  • 357
  • 4
  • 17