0

I'm trying to return an array. Consider the following code.

public Quiz[] getMark(){
}

This is what i have as the return method. I want to return all the elements in the array under this method. I have to use return and the return type has to be Quiz[]

How would i start off?

2
  • 3
    Start off with the Java tutorials so you have the basics. Commented May 20, 2011 at 11:07
  • You posted code is just a definition of a method. After the access modifier 'public' you defined a return value from type of Quiz array. What we are missing is the source object. It does exsisting many ways to transfer elments to your value. Commented May 20, 2011 at 11:10

4 Answers 4

1

May be this:

public Quiz[] getMark(){
  Quiz[] quizs;
  //--do some processing.
  //-- fill data in array
  return quizs;
}
Sign up to request clarification or add additional context in comments.

Comments

0

are you looking for this

public Quiz[] getMark(){

Quiz [] quizArr = new Quiz[5]; quizArr[0]= new Quiz(); ... .. ..

return quizArr; }

2 Comments

This gave me clues to fix it. Cheers
@ART: all other answers are identical to this one.
0

i don't understand what is the problem , arrays in java are objects you just return it

a possible implementation

public Quiz[] getMark(){
    //init the quiz array
    Quiz[] array = new Quiz[5];
    // init each element in the quiz array
    for (int i=0;i<5;++i){
        array[i] = new Quiz();  // could use different constructor
    }
    return array;
}

Comments

0

Is this what you are looking for:

public Quiz[] getMark(){

return quizList.toArray(new Quiz[quizList.size()]()); 

}

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.