Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Question
Book Icon
Chapter 7.6, Problem 7.15CP
Program Plan Intro

Array:

An array is defined as a group that consists of similar data types.

  • The array size must be specified by an “int” value and not long or short.
  • In Java, all arrays are allocated dynamically.
  • An array is always indexed, starting from 0.

Declaring an array reference and creating an array:

  • To insert elements to an array, first an array has to be created by using new operator and then the reference variable is assigned.
  • The syntax is as follows,

arrayRefVar = new elementType[arraySize]

  • From the above statement it is clear that, an array is created using new elementType[arraySize] and then the reference to the newly created array is assigned to the variable arrayRefVar.
  • It is possible to declare an array variable, create an array and assign the reference to a variable in a single statement itself.

The syntax will be as follows,

elementType[]arrayRefVar = new elementType[arraySize];

elementType arrayRefVar[]= new elementType[arraySize);

Example:

double myFile = new double[20];

The statement given above declares an array variable myFile and then creates an array that consists of 20 elements which are of the datatype double and at last assign the reference to myFile.

Explanation of Solution

b. Loop to display each element in the array:

//Class definition

public class Sample {

// define main function

public static void main(String[] args) {

// Declaration of string array

String[] planets = {"Mercury", "Venus", "Earth", "Mars"};

// Loop to read each element in the planets array

for(int i = 0; i<planets.length; i++)

// Display the contents of the planet array

System...

Explanation of Solution

c. Loop to display first character of the elements present in the array:

//Class definition

public class Sample {

// define main function

public static void main(String[] args) {

// Declaration of string array

String[] planets = {"Mercury", "Venus", "Earth", "Mars"};

// Loop to read each element in the planets array

for(int i = 0; i<planets.length; i++)

// Display the contents of the planet array

System.out.println(planets[i]...

Blurred answer
Students have asked these similar questions
True or FalseWhen you call a string object’s Split method, the method divides the string into two substrings and returns them as an array of strings.
The local taqueria now wants some help with their kitchen system.  There are four types of burritos: carnitas, beef, shrimp, and vegetarian. They would like you to create a program to send an alert to the kitchen if there is a shrimp burrito in a given order.   Your main() function should prompt the user for the number of burritos in the order, and then enter all the burrito types for the order.  The burrito types should be stored in an array of strings.  You'll also need to store the number of burritos in the order.  You can assume that no order will have more than 10 burritos.  You can assume that the user only types in valid types of burritos, e.g. carnitas, beef, shrimp, vegetarian. The shrimp burrito must be cooked to order and thus takes longer to prepare than the other kinds.  Because of this, an alert is needed ASAP if there is a shrimp burrito in the order.   Write a function which takes two parameters: your array of burrito strings, and the number of burritos in the array.…
The following statement will declare a one-dimensional String array. What is the subscript for the first element? ​ Dim strLakes(5) As String   a.  5     b.  6     c.  0     d.  1

Chapter 7 Solutions

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

Ch. 7.2 - A program has the following declaration: double[]...Ch. 7.2 - Look at the following statements: int[] a = { 1,...Ch. 7.3 - Prob. 7.13CPCh. 7.3 - Write a method named zero, which accepts an int...Ch. 7.6 - Prob. 7.15CPCh. 7.7 - Recall that we discussed a Rectangle class in...Ch. 7.10 - Prob. 7.17CPCh. 7.11 - What value in an array does the selection sort...Ch. 7.11 - How many times will the selection sort swap the...Ch. 7.11 - Prob. 7.20CPCh. 7.11 - Prob. 7.21CPCh. 7.11 - If a sequential search is performed on an array,...Ch. 7.13 - What import statement must you include in your...Ch. 7.13 - Write a statement that creates an ArrayList object...Ch. 7.13 - Write a statement that creates an ArrayList object...Ch. 7.13 - Prob. 7.26CPCh. 7.13 - Prob. 7.27CPCh. 7.13 - Prob. 7.28CPCh. 7.13 - Prob. 7.29CPCh. 7.13 - Prob. 7.30CPCh. 7.13 - Prob. 7.31CPCh. 7 - In an array declaration, this indicates the number...Ch. 7 - Each element of an array is accessed by a number...Ch. 7 - The first subscript in an array is always. a. 1 b....Ch. 7 - The last subscript in an array is always. a. 100...Ch. 7 - Array bounds checking happens. a. when the program...Ch. 7 - This array field holds the number of elements that...Ch. 7 - Prob. 7MCCh. 7 - This search algorithm repeatedly divides the...Ch. 7 - Prob. 9MCCh. 7 - When initializing a two-dimensional array, you...Ch. 7 - Prob. 11MCCh. 7 - To delete an item from an ArrayList object, you...Ch. 7 - To determine the number of items stored in an...Ch. 7 - True or False: java does not allow a statement to...Ch. 7 - True or False: An arrays sitze declarator can be a...Ch. 7 - Prob. 16TFCh. 7 - True or False: The subscript of the last element...Ch. 7 - Prob. 18TFCh. 7 - True or False: The Java compiler does not display...Ch. 7 - Prob. 20TFCh. 7 - True or False: The first size declarator in the...Ch. 7 - Prob. 22TFCh. 7 - Prob. 23TFCh. 7 - int[] collection = new int[-20];Ch. 7 - Prob. 2FTECh. 7 - Prob. 3FTECh. 7 - Prob. 4FTECh. 7 - Prob. 5FTECh. 7 - The variable names references an integer array...Ch. 7 - The variables numberArray1 and numberArray2...Ch. 7 - Prob. 3AWCh. 7 - In a program you need to store the populations of...Ch. 7 - In a program you need to store the identification...Ch. 7 - Prob. 6AWCh. 7 - Prob. 7AWCh. 7 - Prob. 8AWCh. 7 - Prob. 9AWCh. 7 - Prob. 10AWCh. 7 - Prob. 11AWCh. 7 - Prob. 1SACh. 7 - Prob. 2SACh. 7 - Prob. 3SACh. 7 - Prob. 4SACh. 7 - Prob. 5SACh. 7 - Prob. 6SACh. 7 - Prob. 7SACh. 7 - Prob. 8SACh. 7 - Prob. 9SACh. 7 - Rainfall Class Write a RainFall class that stores...Ch. 7 - Payroll Class Write a Payroll class that uses the...Ch. 7 - Charge Account Validation Create a class with a...Ch. 7 - Charge Account Modification Modify the charge...Ch. 7 - Prob. 5PCCh. 7 - Drivers License Exam The local Drivers License...Ch. 7 - Magic 8 Ball Write a program that simulates a...Ch. 7 - Grade Book A teacher has five students who have...Ch. 7 - Grade Book Modification Modify the grade book...Ch. 7 - Average Steps Taken A Personal Fitness Tracker is...Ch. 7 - Array Operations Write a program with an array...Ch. 7 - Prob. 12PCCh. 7 - Sorted List of 1994 Gas Prices Note: This...Ch. 7 - Name Search If you have downloaded this books...Ch. 7 - Population Data If you have downloaded this books...Ch. 7 - World Series Champions If you have downloaded this...Ch. 7 - 2D Array Operations Write a program that creates a...Ch. 7 - Prob. 18PCCh. 7 - Trivia Game In this programming challenge, you...Ch. 7 - Prob. 20PC
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,