Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class...
-
Upload
branden-gaines -
Category
Documents
-
view
234 -
download
0
Transcript of Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class...
![Page 1: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/1.jpg)
Arrays
![Page 2: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/2.jpg)
Exam 1
1. Java source and executable files have the following extensions?
a. .java and .class
b. .src and .class
c. .javadoc and .exe
d. .list and .exe
![Page 3: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/3.jpg)
Exam 1
2. Which of the following is a Java comment
a. ; This is my first program
b. # This is my first program
c. /* This is my first program */
d. ‘ This is my first program
e. none of the above
![Page 4: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/4.jpg)
Exam 1
3. We use the following operator to create a new object in Java
a. malloc
b. new
c. string
d. newObject
![Page 5: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/5.jpg)
Exam 1
4. Commands to compile and execute a java program are
a. run and java
b. execute and javac
c. javac and java
d. compile and run
![Page 6: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/6.jpg)
Exam 1
5. Identifiers may start with
a. $
b. @
c. a number
d. &
![Page 7: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/7.jpg)
Exam 1
6. It is necessary to declare an object before you can use it
a. True
b. False
![Page 8: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/8.jpg)
Exam 1
7. Which of the following is a valid identifier?
a. Bank Account
b. bank account
c. bank$account
d. bank-account
![Page 9: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/9.jpg)
Exam 1
8. A series of characters that appear in double quote is a char data type
a. True
b. False
![Page 10: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/10.jpg)
Exam 1
9. boolean isDone == false; is a valid assignment statement in Java
a. True
b. False
![Page 11: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/11.jpg)
Exam 1
10. Which of the following is a correct variable declaration statement?
a. int x - float y;
b. int x: float y;
c. int x,y;
d. Long int x;
![Page 12: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/12.jpg)
Exam 1
11. Boolean expressions are built by use of _relational______operators and _boolean_______operators
12. A data value that can not be changed is called __constant____________
13. $Bad-Variable is __bad/invalid/not _ Java identifier.
![Page 13: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/13.jpg)
Exam 1
14. These two data types: __float__, and ___double___are used for real numbers
15. _An object____is an instance of a class
![Page 14: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/14.jpg)
Exam 1
16. double loanPeriod;
if (loanPeriod < 0 || >1000 ); {
System.out.println(“Loan period is invalid”);
System.exit(1);
}
1. loanPeriod is not initialized
2. loanPeriod or a variable name is missing
3. ; is not needed.
![Page 15: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/15.jpg)
Exam 1
17. double s;s = 1.0;switch (s) {case 1.0: System.out.println(“ March madness”); break;case 2.0: System.out.println(“ November rain”); break;case 3.0: System.out.println(“White Christmas”); break;default: System.out.println(“No Special name”); break;}
1. Switch doesn’t support double data type
![Page 16: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/16.jpg)
Exam 1
18. char aChar = ”NFL Championship”;
1. Char datatype can only contain 1 character
2. Char data type needs single quotes instead of double ones
![Page 17: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/17.jpg)
Exam 1
19. int i, j;double x,y;i=1;j=2;x= Math.pow(3, (i/j)); y = x % 2;if (y ==0)System.out.println(x +" is an even number ");elseSystem.out.println(x +" is an odd number");
½ = 0, 30=1.0
y = 1.0 % 2 = 1.0
1.0 is an odd number
![Page 18: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/18.jpg)
Exam 1
20. int count=1, sum=0;
while (count < 9) {
if (count % 2 != 0) {
sum += count;
}
count++;
}
System.out.println(" Sum ="+ sum);
![Page 19: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/19.jpg)
Exam 1
count =1, sum =0, 1 %2 = 1 (!= 0), sum = 0+1=1count =2, sum=1, 2 % 2 = 0count =3, sum=1, 3 % 2 = 1 (!=0), sum = 1+3 = 4count =4, sum=4, 4 % 2 = 0count =5, sum=4, 5 % 2 = 1 (!=0), sum = 4+5 = 9count =6, sum=9, 6 % 2 = 0count =7, sum=7, 7 % 2 = 1 (!=0), sum = 9+7 = 16count =8, sum=16, 8 % 2 = 0count =9, exit
Sum = 16
![Page 20: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/20.jpg)
Exam 1
21. int sum =0;
for (int i=1; i<=2; i++) {
for (int j=1; j<=3; j++) {
sum = sum + (i+j);
}
}
System.out.println(" Sum ="+ sum);
![Page 21: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/21.jpg)
Exam 1
sum =0i=1;
j=1; sum=0+(1+1)j=2; sum =(1+1)+(1+2)j=3; sum = (1+1)+(1+2)+(1+3) = 2+3+4=9
i=2j=1; sum=9+(2+1)j=2; sum=9+(2+1)+(2+2)j=3; sum=9+(2+1)+(2+2)+(2+3)= 9+(3+4+5)=9+12=21
Sum =21
![Page 22: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/22.jpg)
Lab 4
![Page 23: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/23.jpg)
Lesson plan
Arrays
![Page 24: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/24.jpg)
Arrays
We often need to group together related items of data.Cards in a pack.Ships in a port.
Java provides two distinct facilities:Traditional array.Flexible-size collection classes
(java.util).
![Page 25: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/25.jpg)
Problems That Arrays Solve
……
minValue = firstNumber;if (secondNumber < minValue)
minValue = secondNumber;if (thirdNumber < minValue)
minValue = thirdNumber;if (fourthNumber < minValue)
minValue = fourthNumber;if (fifthNumber < minValue)
minValue = fifNumber;
……
What is this code doing?
Finding minimum value from a set of 5 values
![Page 26: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/26.jpg)
Arrays for Numerical data type (primitive data types)
int[] number = new int [5];int minValue;
String inputStr;for (int i=0; i<5; i++) {
inputStr = JOptionPane.showInputDialog(null,"Please enter the value for element "+i);
number[i] = Integer.parseInt(inputStr);}
minValue = number[0];for (int i=1; i<5; i++) {
if (minValue > number[i])minValue = number[i];
}System.out.println(" minValue ="+ minValue);
Array declaration &
allocation memory
![Page 27: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/27.jpg)
Arrays for Numerical data type (primitive data types)
int[] number = new int [5];int minValue;
String inputStr;for (int i=0; i<5; i++) {
inputStr = JOptionPane.showInputDialog(null,"Please enter the value for element "+i);
number[i] = Integer.parseInt(inputStr);}
minValue = number[0];for (int i=1; i<5; i++) {
if (minValue > number[i])minValue = number[i];
}System.out.println(" minValue ="+ minValue);
Getting values for all elements
in the array
![Page 28: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/28.jpg)
Arrays for Numerical data type (primitive data types)
int[] number = new int [5];int minValue;
String inputStr;for (int i=0; i<5; i++) {
inputStr = JOptionPane.showInputDialog(null,"Please enter the value for element "+i);
number[i] = Integer.parseInt(inputStr);}
minValue = number[0];for (int i=1; i<5; i++) {
if (minValue > number[i])minValue = number[i];
}System.out.println(" minValue ="+ minValue);
Access each element of an
array
![Page 29: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/29.jpg)
Arrays
Array is a collection of data values of the same data type.
Example:int[ ] number; // This is an array of integersdouble[] gpa; // This an array of double
![Page 30: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/30.jpg)
Array Declaration
Format:<data type>[] <array_name>;
OR<data type> <array_name>[];
data type: double, int, float, stringExample:
double[] gpa;Or
double gpa[];
![Page 31: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/31.jpg)
Arrays
The amount of memory allocated to store an array depends on the size and type of values in the array Size: number of elements in the array Type of values: data type of each element in the array An individual value in an array is called array element
Example:int[ ] number = new int[5];data type: integer (4 bytes)size: 5memory: 20 bytes
Array is a reference data type. Array is NOT an object
![Page 32: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/32.jpg)
Arrays
Example:
int[ ] number = new int[5];
number
1 2 30 4
number[0]
6
![Page 33: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/33.jpg)
Arrays
Elements of an array are indexed from zero to size -1
Size: the number of elements in an array length: a public constant represents the size of
an array.Example:
sum =0;for (int i=0; i<number.length; i++)
sum += number[i];
![Page 34: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/34.jpg)
Fixed –size array declaration
Size of an array is pre-determined.Example:int[] number= new int[5];
Problems:• What if we have more than pre-
determined size of an array?• Underutilization of space.
![Page 35: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/35.jpg)
Variable-size array declaration
In Java, we can declare an array of different size every time we run a program
Example:int size;int[] number;
inputStr = JOptionPane.showInputDialog(null,"Please enter the number of elements ");size = Integer.parseInt(inputStr);
number = new int[size];
![Page 36: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/36.jpg)
Practice (in class and/or at home)
Modify the class ComputeMin (week 4) to print out the minimum of an array with n integers.
![Page 37: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/37.jpg)
Arrays for Objects
// An array for the names of the distinct private Loan[] loanArray = new Loan[5];
• Note: No Loan objects are created. Only a container for Loan.
• Each location is initialized to null.loanArray
NULL
![Page 38: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/38.jpg)
Arrays of Objects
private Loan[] loanArray = new Loan[5];
for (i=0; i<5; i++) {
loanArray[i] = new Loan();
}
loanArray
![Page 39: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/39.jpg)
Indexing an Array
int[] number = new int [5];
number[0] =2;
number[1]=3;
number[2]=4;
number[3]= 6;
number[4]=-1;
![Page 40: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/40.jpg)
Initializing Arrays
... // Differentially number the assignments. private int[] number = {1, 2, 1, 5, 1,}; private final int totalNumber = number.length;
• No new required.• Terminating semicolon.• Optional final comma.
![Page 41: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/41.jpg)
Further Initializer Examples
String[] suitNames = { "Spades", "Hearts", "Diamonds", "Clubs"};
Point[] vertices = { new Point(0,0), new Point(0,1), new Point(1,1), new Point(1,0),};
YearlyRainfall y2k = new YearlyRainfall( new int[]{10,10,8,8,6,4,4,0,4,4,7,10,});
![Page 42: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/42.jpg)
Note for project 2
a. For each class (Loan and Amortization), list:data members:
data type, namemethods:
return data type, name, parametersconstructor
parameters
![Page 43: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/43.jpg)
Note for project 2
b. For Amorization main list
methods:
return data type, name, parameters
Draw a diagram
c. Submit your code
![Page 44: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/44.jpg)
Test case
Interest rate = 0.4% (0.004)
Number of months = 12
Loan amount = 100
Amortization payment = $8.55
![Page 45: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/45.jpg)
Iterating over an Array in Reverse
int size = 5;minValue = number[size-1];for (int i=size-2; i>=0; i--) {
if (minValue > number[i])minValue = number[i];
}
![Page 46: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/46.jpg)
Copy an array
Use arraycopy method from System class.
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
![Page 47: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/47.jpg)
Copying Arrays
int size=5;int number[] = new int[size];
// Assuming that we get values for number array here
// Declare another array called number1 int number1[] = new int[size];
// Copy the values of array number to array number1System.arraycopy(number,0,number1,0,size);
![Page 48: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/48.jpg)
Passing Array to Methods
When an array is passed to a method, only its reference is passed. A copy of the array is not created in the method.
That means:
we pass the identifier for that array which in fact is a reference to a start address of the array.
![Page 49: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/49.jpg)
Passing Array to Methods
Assuming changeArrayValue is one method of a class named ArrayClass
public void changeArrayValue(int[] arrayChanged) {for (int i=0; i< arrayChanged.length; i++)
arrayChanged[i] += 1;
}
We call this method as:
![Page 50: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/50.jpg)
Passing Array to Methods
ArrayClass anObj = new ArrayClass();
int number[5] = new int[5];
for(int i=0; i<number.length; i++)
number[i] = i;
anObj.changeArrayValue(number);
![Page 51: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/51.jpg)
Passing Array to Methods
for(int i=0; i<number.length; i++)
number[0] = i;
anObj.changeArrayValue(number);
0 1 2 3 4
1 2 3 4 5
number
number
![Page 52: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/52.jpg)
“Just in time” overview
1. It is acceptable to have an array containing values of both type BYTE and type INT, as long as all of the values are integers:
a. True
b. False
![Page 53: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/53.jpg)
“Just in time” overview
1. It is acceptable to have an array containing values of both type BYTE and type INT, as long as all of the values are integers:
a. True
b. False
![Page 54: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/54.jpg)
“Just in time” overview
2. A code fragment that declares an array of type long named accounts is
a. long accounts;
b. long[ ] accounts;
c. long{ } accounts;
![Page 55: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/55.jpg)
“Just in time” overview
2. A code fragment that declares an array of type long named accounts is
a. long accounts;
b. long[ ] accounts;
c. long{ } accounts;
![Page 56: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/56.jpg)
Matching game (again)
3. A code fragment that declares an array named grades and allocates the memory to store 15 values of type double:
= ;
A.[15] B. new C. gradesD. double E. double[ ]
![Page 57: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/57.jpg)
Matching game (again)
4. A code fragment that declares an array named grades and allocates the memory to store 15 values of type double:
= ;
A.[15] B. new C. gradesD. double E. double[ ]
E BC D A
![Page 58: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/58.jpg)
Searching an Unsorted Array
We often need to search an array for a particular item of data.
The data is often unsorted.The item might or might not be present.
Care must be taken not to search beyond the end of the array.
We need to decide how to return a found item.
![Page 59: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/59.jpg)
Search with Multiple Returns
public int indexOf(int[] numbers,int value){ final int notPresent = -1; for(int index = 0; index < numbers.length; index++){ if(numbers[index] == value){ return index; } } // We did not find it. return notPresent;}
![Page 60: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/60.jpg)
The Arrays Class
Defined in the java.util package.Contains static methods for manipulating
arrays:binarySearch: search for a value.equals: compare the contents of two
arrays.fill: fill an array with a particular value.sort: sort the contents of an array.
![Page 61: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/61.jpg)
Multi-Dimensional Arrays
Arrays of multiple dimensions are possible.2D grid, for a board game such as chess.3D cube structure, etc.
Multi-dimensional arrays are regarded as being arrays of arrays.
Non-rectangular structures are possible.
![Page 62: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/62.jpg)
2D Array Construction
final int numRows = 10, numCols = 5;double[][] matrix = new double[numRows][numCols];
char[][] hiddenWord = { { 'd', 'g', 'i', 'b' }, { 'e', 'i', 'u', 'm' }, { 't', 'a', 's', 'a' },};
![Page 63: Arrays. Exam 1 1. Java source and executable files have the following extensions? a..java and.class b..src and.class c..javadoc and.exe d..list and.exe.](https://reader036.fdocuments.net/reader036/viewer/2022062300/56649e405503460f94b31f49/html5/thumbnails/63.jpg)
Review
Arrays make it possible to group related items in a single object.
An array's length is fixed on construction.Arrays may have multiple dimensions.