Java Arrays

Ace your homework & exams now with Quizwiz!

If an array has 5 elements, what is the index value for the last item in the array?

4 (it starts at 0)

What would x = d.length give for the value of x? d[ ] = {2,5,7,1,10};

5

Suppose you are thinking of a number in a binary sort from 0 to 100, what is the first number that it's compared to?

50

Which way can be used if the data is unorganized?

Linear

List two ways to search a data set

Linear and Binary

Suppose data is the string "open now." What does ch = data.charAt(8) return?

Returns the character

What does ch = data.charAt(0) in the string "open now." return?

Returns the o

Suppose the test is the string supal, what value does y have?

false all the characters must match

How are arrays implemented?

Methods

Can the same array hold both the name of a student and their grades?

No mixing

What describes the position of an element within an array?

index

How can you declare and initialize a one-d array called d with the values {2, 5, 7, 1, 10} using one line of code?

int d[ ] = {2,5,7,1,10};

What type of array would you use to hold all of your final exam grades (assume just 1st and 2nd semester) for all of your high school years. Write a piece of code to declare it.

int g[][][] = new int[3][2][4]; this is a 3 dimensional array

Write a method to find the minimum value in a one dimensional array.

int min = 10000; for (int i=0; i< 10; i++) if (sample[i] < min) min = sample[i];

What is an array?

a set of data of the same type referenced with a common name

Suppose you have two arrays num1 and num2. What does num1 = num2 do?

all values at the various positions of num1 match num2, you are causing num1 to reference num2

How do you declare a one-dimensional array to hold ten characters?

char sample[ ] = new char [10];

Even though will typically use one step to declare an array, the creation of an array is a two step process, what are the two steps?

int sample[ ]; sample = new int[10]

What single line of code can be used to declare a one D array called temp to hold 3 items?

int temp = new int[3];

Example of hard coding numbers into an array (2x3x2)

int[][][] = { { {0,12,4}, {4,16,5}, {8,20,6}}, {{2,14,7}, {6,18,8}, {10,22,9}} };

What type of an array would you use to hold the hours you work each day for a part time job (assume you work diff numbers of hours each day)?

irregular

List two advantages of storing data with arrays

it's easily manipulated, sorted easily, tallied easily, searched easily

Suppose data is the string "Broken" What is the value of part in the expression part =data.substring(3,6)? What type of variable is part?

part would be a string and in this case ken

Give an example of a need to use a 3-d array.

recording height, weight, blood pressure

What does the following do, y = test.equals("sup"), and what type of variable is y?

return true if match, the variable y is boolean

Explain how a binary search works

starts with a sorted list and cuts in half over and over while making comparisons

How can the contents of a string variable be altered?

string are immutable, contents can't be altered

What type of an array would you use to hold the weight and height of students in the class of 2010?

two dimension [22][2] in this case the row is the student number, if you had 22 in the class

Suppose data is the string "BrokenBroken" What is the value of x in the expression x =data.lastIndexOf("on")?

x = -1 since it's not found

. Suppose data is the string "BrokenBroken" What is the value of x in the expression x =data.lastIndexOf("en")?

x = 10 (the last makes it find the last en)

Suppose data is the string "BrokenBroken" What is the value of x in the expression x =data.indexOf("en") ?

x = 4 finds the first match

. If data = "Z", what is the value of x in the expression x = data.compareTo("B") ?

x = some positive number (x=0? @Nov2 ex 2 notes)

If x = bus.length, and y = bus[0].length, z = bus[1].length, what are the values for x , y, and z?

x =7, y = 4, z = 2

Suppose data is the string "Broken" What is the value of x in the expression x = data.compareTo("Summer") ?

x will be some negative number because broken is shorter than summer

How can you search for a period in a sentence?

y = sentence.indexOf("."), ch = sentence.charAt(".");

Can an array hold strings?

yes

How many loops are necessary in the Bubble sort routine?

2

How many data points can array that's 2x3x4 hold?

24 (2x3x4=24)

What kind of number is the index value for an array?

0 or a positive integer (no negatives or decimals)

What is at location g[1][2][0]?10 What about g[0][1][1]? int[][][] = { { {0,12,4}, {4,16,5}, {8,20,6}}, {{2,14,7}, {6,18,8}, {10,22,9}} };

10 and 16

Which way can be used if the data is organized?

Binary

Explain how a linear search works

Goes through items and compares

Give three examples where you might use arrays

Grades, bus schedule, race times, wind data

What is the term that describes the items that is used to access a value in an array?

Index

What message do you get if you go beyond the index range of an array?

Index out of bounds

Create and fill an array with the strings "This", "is" "great"

String data[] = {"This", "is", "great"};

Create a string that has no characters

String empty = "";

Create an appropriate array to hold the names and phone numbers of 3 people.

String numbers[][] = { {"Tom", "555-3322"}, {"Sam", "555-8976"}. {"Cole", "596-2142"} };

Can a string have just one character? Show an example

String one = "A"; char one = 'A';

Create a string with the characters, "Hello"

String str = "Helllo";

List two ways to construct a string.

String str = new String("Hello"); String str = "Helllo";

If you have two arrays, one called apple, and one orange, does this work: apple = orange:;

Yes

What is an irregular array and give an example of declaring one called bus for a bus schedule that just runs M, W, F for 4 times each day, on Tues, Thurs, and Sat twice, and Sun once.

an array that has a diff num of columns for each row int bus[][] = new int[7][]; bus[0] = new int[4]; //on Mondays you can hold 4 data pts bus[1] = new int[2]; // on Tuesday, 2 data pts bus[2] = new int[4]; and so on

Explain what a Bubble sort is and how does it word? How many loop do you need?

compares values at two index locations and does a swap if necessary, it repeats this over and over, need two loops

Write a piece of code to initial an array called g that's 2x3x2 to even numbers starting with zero. n=0;

for (int d=0; d< 2; d++) for (int c=0; c< 3; c++) for (int r=0; r<2; r++) g[r][c][d] = n; n = n + 2;

Write a piece of code to initialize the five elements of a one-d array to the int value of 10.

for (int i=0; i<5; i++) sample[i] = 10;

What are strings in Java? objects, arrays, or characters?

objects

. How many loops are necessary to find the largest number in an array?

one

How can you tell if a character is capitalized?

the decimal value is between 65 and 90 inclusive (I think this is referring to the ascii chart)

Why are arrays convenient?

they group together related data, they organize data, they assist in the sorting and searching of data

Suppose data is the string "finish now" What does the following do, z = data.length(), and what type is the variable z?

z is an integer and in this case z = 10


Related study sets

Macro Midterm #5 (Ch 15,16,17, & 18) NOTES

View Set

ISTQB Foundation Extension Agile Tester Chapter 3: Agile Testing Methods, Techniques, and Tools

View Set

(Chapter 16 HW) Exchange Rates and International Capital Flows

View Set

The Great Gatsby Vocabulary Chapters 1-3

View Set