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

Java - Choices: Which of the following array code fragments correctly corresponds to the given pseudocode? Read in the number of players (assuming variable in references console input) Declare and initialize an array to hold the names of the players

OpenStudy (alprincenofl):

http://prntscr.com/5f1q2m

OpenStudy (woodrow73):

You initialize an array in the following format: ``` int numOfArrayElements = 2; String[] strArrayVariable = new String[numOfArrayElements]; //now you've essentially created 2 string variables -- //one called //strArrayVariable[0]; //and the other called //strArrayVariable[1]; //now I''ll assign them both to a string value: strArrayVariable[0] = "Hello"; String space = " "; //holds a space strArrayVariable[1] = "World"; //and display System.out.print(strArrayVariable[0] + space + strArrayVariable[1]); ``` Your question reads in from the keyboard; if you are familiar with the scanner class, then you can use process of elimination -- if not, I can teach you the scanner class if you want.

OpenStudy (woodrow73):

note the [] after the data type, and the [] again at the end.. I'll show some other examples: ``` int[] intArray = new int[5]; //5 arrays intArray[0] - intArray[4] double[] dubArray = new double[6323]; //6323 arrays dubArray[0] - [6322] String[] strArray = new String[1]; //1 string array of strArray[0] //now I'll extract the number of elements of each array: int intArrayElements = intArray.length; int dubArrayElements = dubArray.length; int strArrayElements = strArray.length; System.out.println((intArrayElements + dubArrayElements + strArrayElements)); //prints out 6329 //notice none of the arrays hold values yet.. now I'll //assign 1 - 6323 to the dubArray element for(int i = 0; i < 6323; i++) dubArray[i] = i+1; //now dubArray holds 1-6323 //now print out the 6323 numbers in ascending order: for(int i = 0; i < 6323; i++) System.out.println(dubArray[i]); ```

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!