Question: Q1) How would you declare an array of doubles called myDoubles? Arrays can be initialized in one of two ways. In one method, the array

Q1) How would you declare an array of doubles called myDoubles? Arrays can be initialized in one of two ways. In one method, the array elements are placed in a list enclosed in curly braces after the array name denition. For example, the code below creates an array of ints with 3 elements: 1, 2 and 3. int[] a = {1, 2, 3, 4}; We can also initialize an array with a new construct, indicating how many elements we want with a number inside square brackets, as in the example double[] b = new double[43]; When this type of initialization is used, the values of the array are initialized to zero for numeric types, false for booleans, and null for object types (such as String).

Q2) What values would the elements of the following array have initially? int[] q = new int[99];

Q3) What values would the elements of the following array have initially? String[] s = new String[10]; Iterating Through Arrays Arrays in Java use zero-based indexing. You can get how many elements are in an array by using the .length property. To get the length of an array s, you would write s.length. Note that this is not a method, so you dont need parentheses. To iterate through an array of data, typically a for loop is used. For example for(int i=0; i

Q4) Write a for loop to add 2 to all the elements of an array called y in lab3answers.txt. One way to instantiate an array is with a new command. Another way is to include a list of items in curly braces. Type in or copy the following program and save it in the le

public class Lab3a {

public static void main(String [] args)

{ int a[] = {0,1,2,3};

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

{ System.out.println("a["+i+"] = "+a[i]); }

}

}

Compile and run the program.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!