Java Reference
In-Depth Information
You can also use the
toString
method to return a string that represents all elements in the array.
This is a quick and simple way to display all elements in the array. For example, the following code
toString
int
[] list = {
2
,
4
,
7
,
10
};
System.out.println(Arrays.toString(list));
displays
[2, 4, 7, 10]
.
7.26
✓
✓
What types of array can be sorted using the
java.util.Arrays.sort
method?
Does this
sort
method create a new array?
Check
Point
7.27
To apply
java.util.Arrays.binarySearch(array, key)
, should the array
be sorted in increasing order, in decreasing order, or neither?
7.28
Show the output of the following code:
int
[] list1 = {
2
,
4
,
7
,
10
};
java.util.Arrays.fill(list1,
7
);
System.out.println(java.util.Arrays.toString(list1));
int
[] list2 = {
2
,
4
,
7
,
10
};
System.out.println(java.util.Arrays.toString(list2));
System.out.print(java.util.Arrays.equals(list1, list2));
The
main
method can receive string arguments from the command line.
Key
Point
Perhaps you have already noticed the unusual header for the
main
method, which has the
parameter
args
of
String[]
type. It is clear that
args
is an array of strings. The
main
method is just like a regular method with a parameter. You can call a regular method by pass-
ing actual parameters. Can you pass arguments to
main
? Yes, of course you can. In the fol-
lowing examples, the
main
method in class
TestMain
is invoked by a method in
A
.
VideoNote
Command-line arguments
public class
A {
public static void
main(String[] args) {
String[] strings = {
"New York"
,
"Boston"
,
"Atlanta"
};
TestMain.main(strings);
public class
TestMain {
public static void
main(String[] args) {
for
(
int
i =
0
; i < args.length; i++)
System.out.println(args[i]);
}
}
}
}
A
main
method is just a regular method. Furthermore, you can pass arguments from the
command line.
7.13.1 Passing Strings to the
main
Method
You can pass strings to a
main
method from the command line when you run the program.
The following command line, for example, starts the program
TestMain
with three strings:
arg0
,
arg1
, and
arg2
:
java TestMain arg0 arg1 arg2
arg0
,
arg1
, and
arg2
are strings, but they don't have to appear in double quotes on the
command line. The strings are separated by a space. A string that contains a space must be
enclosed in double quotes. Consider the following command line:
java TestMain "First num" alpha 53
Search WWH ::
Custom Search