Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (alprincenofl):

What is the output of the following array list code fragment? ArrayList players = new ArrayList(); System.out.println(players.get(0));

OpenStudy (alprincenofl):

Screenshot & Choices: http://prntscr.com/5ftuo4

OpenStudy (alprincenofl):

This is Java

OpenStudy (harsha19111999):

To show you what happens, I have written a very simple program

OpenStudy (harsha19111999):

public class Test { public static void main(String args[]) { int Array[] = {}; System.out.println(Array[0]); } }

OpenStudy (harsha19111999):

Actually, there's no error in the program. The program complies successfully

OpenStudy (alprincenofl):

I understand everything of the code you wrote except: int Array[] = {}; what is the output then? what will this line print?

OpenStudy (harsha19111999):

But there are no elements in the array. So, you would get ArrayIndexOutOfBoundsException

OpenStudy (harsha19111999):

That is the 2nd option

OpenStudy (harsha19111999):

int Array[] = {}; This is used to declare a array

OpenStudy (alprincenofl):

Thanks a lot I understand and know how to println a text but don't understand this point you are explaining http://prntscr.com/5ftz1x

OpenStudy (alprincenofl):

Now I got it, thanks

OpenStudy (harsha19111999):

There is another process: int Array[] = new Array[2];

OpenStudy (harsha19111999):

The code which I first wrote: int Array[] = {1, 2, 3, 4} This is called an auto array. Instead of creating a new array, you can use this

OpenStudy (harsha19111999):

You're welcome

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!