We appreciate your visit to When programming it is often useful to have a large number of variables Rather than declaring all of these variables one by one you can. This page offers clear insights and highlights the essential aspects of the topic. Our goal is to provide a helpful and engaging learning experience. Explore the content and find the answers you need!

When programming, it is often useful to have a large number of variables. Rather than declaring all of these variables one by one, you can use an array to declare a large number of variables in one swoop. An array simply acts like a list of variables. The array can have any length (i.e. number of variables) that you want. For example, the following code declares studentGrades to be an array of 35 doubles.


//Declare studentGrades as an array of doubles

double[] studentGrades;

//Make it an array of size 35.

studentGrades = new double[35];

The array has 35 elements, each of which acts like a variable. We start count with 0, so the elements are numbered 0 through 34. Here is how to set the first element to 97.3, and the second element to 88.0:


studentGrades[0] = 97.3;

studentGrades[1] = 88.0;

The following code prints out the value that is currently in studentGrades[0]


ConsoleIO.printLine(studentGrades[0]);

As you can see, you can use an array element just as you would any other variable. You just need to indicate which one you are talking about by providing an index. A student of mine once likened it to a pile of plates. You don’t have a different name for each plate. You collectively have a name for the pile, and the plates are numbered 0, 1, 2, etc.


Here is the general syntax for declaring an array – notice it is just like a normal variable declaration, except you have the []


dataType[] arrayVariableName;

Here is the general syntax for creating an array, once an array variable for that data type is already declared:


arrayVariableName = new dataType[arraySize];

Complete the exercises as indicated in Worksheet9.java.


public class Worksheet9 {


public static void main(String[] args) {


//Declare an array of doubles. Set it to be an array of length 100.


//Declare an array of ints, and make it an array of length 20000.


//Declare an array of booleans. Set it to be a new array of length 10.


//Declare another array of ints, this time with only 3 elements.


//Set the elements to be 35, 77, and 89.


//Finally, print the last element of the array.


}


}

Answer :

The given code demonstrates the use of arrays in Java. It shows how to declare an array, set its length, assign values to its elements, and access specific elements using index notation. The code also includes exercises to declare arrays of different data types and lengths and set values to their elements.

The code starts by declaring an array of doubles named "studentGrades" and sets its length to 100 using the syntax "new double[100]". It then declares an array of ints named "arrayOfInts" with a length of 20000 and declares another array of booleans named "arrayOfBooleans" with a length of 10 using the syntax "new boolean[10]". Next, it declares an array of ints named "anotherArrayOfInts" with a length of 3 and assigns the values 35, 77, and 89 to its elements. Finally, it prints the last element of the "anotherArrayOfInts" array using index notation (anotherArrayOfInts[2]).

Arrays are useful when you need to store and access a collection of values of the same data type. The length of an array specifies the number of elements it can hold, and the elements can be accessed using indices starting from 0. Arrays provide a convenient way to store and manipulate multiple values within a single variable, making it easier to manage large sets of data.

Learn more about booleans here:

https://brainly.com/question/30882492

#SPJ11

Thanks for taking the time to read When programming it is often useful to have a large number of variables Rather than declaring all of these variables one by one you can. We hope the insights shared have been valuable and enhanced your understanding of the topic. Don�t hesitate to browse our website for more informative and engaging content!

Rewritten by : Barada