0

i want to pass an array of objects i have stored in one for loop to a second for loop in another method to display the contents. e.g:

public static Student[] add()
for(int i = 0; i < studentArray.length; i++)
        {
            System.out.print("Enter student name ");
            studentName = EasyIn.getString();
            System.out.print("Enter student Id ");
            studentId = EasyIn.getString();
            System.out.print("Enter mark ");
            studentMark = EasyIn.getInt();
            studentArray[i] = new Student(); //create object
            tempObject = new Student(studentName,studentId,studentMark);
            place = findPlace(studentArray,studentName, noOfElements);
            noOfElements = addOne(studentArray, place, tempObject, noOfElements);   
        }

into here

public static void displayAll()
{
Student[] anotherArray = add();
    for(int i = 0; i < anotherArray.length ; i++)
    {
        System.out.print(anotherArray[i].toString());
    }   
}

to call it in a menu here:

                        case '3': System.out.println("List All");
                                  displayAll();
                                  EasyIn.pause();

when i press 3 on the menu it just calls the add method again but when i add in the values into the array again then it displays the array. i just want to display the array only

6
  • Your code won't compile. In your add() method you should put curly brackets { and }, and it should return somethinf of Student[] type. Commented Apr 30, 2012 at 10:47
  • The question is unclear, would you clarify the behavior that you don't want? It is that each time you call add() you are reconstructing the array and you only want this to happen once? Commented Apr 30, 2012 at 10:47
  • they are just a sample of the code i have shown, shall i paste all the code i have done and show you? Commented Apr 30, 2012 at 10:48
  • @ john B each time i call the display method it recontructs the array and then displays it. i just want to display it. Commented Apr 30, 2012 at 10:50
  • If add() reconstructs the array, and you call add() at the begining of displayAll() (inside it), every time you call displayAll(), you'll be reconstructing the array. Try @mcfinnigan or @Rahul Borkar solutions. Commented Apr 30, 2012 at 10:54

3 Answers 3

2

Similar to others

public void displayAll(Student... students) {    
    for(Student student:students)
        System.out.print(student+" "); // so there is space between the toString
    System.out.println();
}

or

public void displayAll(Student... students) {    
    System.out.println(Arrays.asList(students));
}
Sign up to request clarification or add additional context in comments.

Comments

1

change displayAll() to take the array as a parameter:

public void displayAll(Student[] students) {
  ...
}

and call it as follows:

Student [] students = add();

...

case '3': System.out.println("List All");
    displayAll(students);
    EasyIn.pause();

1 Comment

well it works to a certain degree, each time it calls the Student [] students = add(); it calls the add() method for some strange reason
1

You can change your displayAll method definition to

public static void displayAll(Student[] anotherArray)
{    
    for(int i = 0; i < anotherArray.length ; i++)
    {
        System.out.print(anotherArray[i].toString());
    }   
}

and then call add method from wherever you want to call before switch case and call displayAll method with student[] as parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.