0% found this document useful (0 votes)
32 views2 pages

A4

The document discusses filling arrays with a specified value, converting arrays to strings, and passing command line arguments to the main method as a string array. Command line arguments are stored in the args array, with the first argument at args[0] and subsequent arguments at higher indices.

Uploaded by

myob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

A4

The document discusses filling arrays with a specified value, converting arrays to strings, and passing command line arguments to the main method as a string array. Command line arguments are stored in the args array, with the first argument at args[0] and subsequent arguments at higher indices.

Uploaded by

myob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Fill:

Fill an array with some specified value.


public void fill(int [], v) //Signature...fill array a with value v.

Example:

int pk[] = {1, 2, 3, 4, 5};

19-3

19-4 Arrays.fill(pk, 77); //Array now looks like this {77, 77, 77, 77, 77}

String equivalent:

An entire array can be converted to a String similar to “[2, -3, 5, 18, 22]”. Example:
Arrays.toString(myArray); //Typically printed as a test

The above discussion is for the int type arrays; however, all methods work for arrays of any
of the primitive types and Strings. The sort method works for objects from any class
implementing the Comparable interface... All methods are static.

Command Line arguments:

Let’s take a final look at the signature for the main method: public static void main(String
args[])

Now that we know about arrays, we can see that “String args[ ]” is declaring args as a String
array. But where and how is this args[ ] array to be used? (Incidentally, this args[ ] array
could be called by any legal variable name.)

The args[ ] array allows us to pass command line arguments to the main method. Entering a
command line (see Appendix X) at the DOS prompt is one way to run a Java program. To do
this you would need to be in a DOS console via the sequence Start | Run | cmd (don’t use the
older command) | OK):

java MyClass -46 Fleetwood.bat

What exactly does all this mean? The leading word java means to run the Java executable file
(java.exe), MyClass (shown below) is the class containing the main method you wish to run,
-46 is a String representing the first parameter we are passing ( stored in args[0] ), and
Fleetwood.bat is a String representing the second parameter we are passing ( stored in args[1]
).

You might also like