Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: help with array for creating and inserting reference to object in java

  1. #1
    Junior Member
    Join Date
    Apr 2012
    My Mood
    Thanked 0 Times in 0 Posts

    Post help with array for creating and inserting reference to object in java

    i'm doing heapSort algorithm. where I have to creat an array of reference to object. I actually creaed array which is empty so println displays null. thats fine. but when I actually add object(each listing object contain information of student-their name, id#, and gpa) and try to print it, it gives me result. My problem is I want to use toString method coded in Listing class(student information) to print all information. But it gives me all information when I directly give array element to print statement. I think this would no longer keep my code encapsulated.
    System. out.println(data[j]); & System.out.println(data[j].toString); this both gives me same output.
    I dont know whether it is correct or wrong. I would be grateful if anyone can explain me difference between both eprint statements and how encapsulation will work here. I'm also including code that I've tried.
    import java.util.Scanner;

    public class MainHeapSort {

    public static void main(String[] args){

    Scanner input = new Scanner(System.in);

    System.out.println("Enter number of students: ");
    int size = input.nextInt();

    Listing[] data = new Listing[size];// data of class listing that stores students information

    //following two statements says that data array is empty hence prints null
    //for(int j = 0; j < size; j++){

    Listing temp = new Listing("","","");

    for(int j = 0; j < data.length ; j++){

    System.out.println("Enter name of the student: ");
    String stuName = input.next();
    System.out.println("Enter student ID number: ");
    String idNumber = input.next();
    System.out.println("Enter students GPA: ");
    String stuGpa = input.next();

    Listing student = new Listing(stuName, idNumber, stuGpa);
    data[j] = student.deepCopy();

    // this is swap just for my understanding.
    temp = data[0];
    data[0] = data[2];
    data[2] = data[1];
    data[1] = temp;

    for(int j = 0; j < size; j++){
    System.out.println("after changing index: ");



    here is the code for class Listing that stores student's info
    public class Listing
    { private String name; // key field
    private String address;
    private String number;

    public Listing(){}

    public Listing(String n, String a, String num )
    { name = n;
    address = a;
    number = num;

    public String toString( )
    { return("Name is " + name +
    "\nAddress is " + address +
    "\nNumber is " + number + "\n");

    public Listing deepCopy( )
    { Listing clone = new Listing(name, address, number);
    return clone;

    public int compareTo(String targetKey)
    { return(name.compareTo(targetKey));

    public void setAddress(String a) // coded to demonstrate encapsulation
    { address = a;

    public String getKey()
    { return name;
    }// end of class Listing

    when I used Listing[] data array in other coding I used it as private. I dont know but I'm not able to use it as private in main method. it gives me an error.

    if any body can help me understand this I would be really greatful.
    Thank you

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,705 Times in 2,655 Posts

    Default Re: help with array for creating and inserting reference to object in java

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting. Unformatted code is hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: February 14th, 2012, 07:42 AM
  2. Object Reference
    By Mr.777 in forum Object Oriented Programming
    Replies: 2
    Last Post: June 13th, 2011, 03:03 AM
  3. Passing reference via object
    By Stefan_Lam in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2011, 11:57 AM
  4. Object as a Reference into Object's Class
    By Ace Coder in forum Object Oriented Programming
    Replies: 6
    Last Post: November 30th, 2010, 12:22 PM
  5. Object as Reference not working
    By jassi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 9th, 2010, 09:47 AM