Java Reference
In-Depth Information
if (index != -1)
//Line 18
System.out.println("Line 19: " + number
+ " is found at position "
+ index);
//Line 19
else
//Line 20
System.out.println("Line 21: " + number
+ " is not in the list.");
//Line 21
}
//Line 22
}
//Line 23
Sample Run 1: (In this sample run, the user input is shaded.)
Line 10: Enter 10 integers.
2 56 34 25 73 46 89 10 5 16
Line 14: Enter the number to be searched: 25
Line 19: 25 is found at position 3
Sample Run 2:
Line 10: Enter 10 integers.
2 56 34 25 73 46 89 10 5 16
Line 14: Enter the number to be searched: 38
Line 21: 38 is not in the list.
In this program, the statement in Line 7 creates intList to be an array of 10 elements.
The for loop in Lines 11 and 12 inputs the data into intList . The statement in Line 14
prompts the user to enter the search item; the statement in Line 15 inputs this search item
into number . The statement in Line 17 uses the method seqSearch to search intList
for the search item. In Sample Run 1, the search item is 25 ; in Sample Run 2, it is 38 .
The statements in Lines 18 through 21 output the appropriate message. Notice that the
search in Sample Run 1 is successful, but in Sample Run 2 it is unsuccessful.
Arrays of Objects
In the previous sections, you learned how to use an array to store and manipulate values of
the primitive data types, such as int and double . You can also use arrays to manipulate
objects. This section explains how to create and work with arrays of objects.
Arrays of string Objects
This section discusses how to create and work with an array of String objects. To create
an array of strings, you declare an array as follows:
String[] nameList = new String[5];
//Line 1
This statement declares and instantiates nameList to be an array of 5 elements, wherein each
element of nameList is a reference to a String object. (Note that this statement only creates
 
 
Search WWH ::




Custom Search