I have started to learn JAVA and I learnt that the main method

public static void main (String [] chpt)

ARGS often written fondly by coders can also be written changed to any word you want. ARGS or CHPT in my declaration is supposed to be an array. How can I view the contents of this array? I tried

System.out.println(Arrays.deepToString(chpt)); and
System.out.println(Arrays.toString(chpt));

This is the entire program

public class Dislpy{    
static int square(int num) {
    // TODO Auto-generated method stub
    return num *num;

}
public static void main(String[] chpt) {
    // TODO Auto-generated method stub
    int num = 12;

    int counter = chpt.toString().length();
    System.out.println("Squared is " +square(num) +" " +counter);
    System.out.println(Arrays.deepToString(chpt));
     for (int i = 0; i<= counter; i++){
        System.out.println("over here "+chpt.toString().indexOf(i));
    }
   }
}

but it didn't work and the output is

Squared is 144 27
[]
Over here -1.................this line was printed 27 times.

What is 27? What does 27 signify here? As @peeskillet mentioned in his answer there are no arguments being passed through command line hence it wont display any arguments.

I would like to access the contents of chpt array. Help me understand this better. Thanks, Cheers!

The output as displayed in Eclipse designmonks

有帮助吗?

解决方案

Updated answer

This works:

public static void main(String[] chpt) {
    int counter = Integer.parseInt(chpt[0]);
    for (int i = 0; i <= counter; i++) {
        System.out.println("over here " + square(i));
    }
}

over here 0
over here 1
over here 4
over here 9

Array length

The magic 27 from your code is String length, not Array size:

chpt.toString().length() == 27
"[Ljava.lang.String;@5dcba031".length() == 27

The proper way is

System.out.println(chpt.size());

Printing arrays

With arrays it is:

public static void main(String[] args) {
    System.out.println(java.util.Arrays.toString(args));
}

args is primitive type String[]

System.out.println(Arrays.toString(args)); 

and whent printed results with something like:

[Ljava.lang.String;@5dcba031

but after conversion to list"

System.out.println(Arrays.toString(args));

prints all elements nicely

其他提示

for (String argument : chpt) {
    System.out.println(argument);
}

Run this test

file TestClass.java

public class TestClass {
    public static void main(String[] args){
        if (args.length == 0){
            System.out.println("You need at least one arg dummy!");
            System.exit(0);
        } else {
            for (String s : args){
                System.out.println(s);
            }
        }
    }
}

Save the file as TestClass.java
Go to your command line and go to the directory of the java file and type:
javac TestClass.java
Then type:
java TestClass "Hello, world!" "Hello, Dummy!" "Where are my Dragons?!"

See what you get.

Each argument passed to the command line is separated by a space. If I left the quotation marks out, there would be 8 arguments instead of 3.

String [] chpt// would print out [] as it contains no values nor are values added to it.

just try this

for(String s : chpt){
   System.out.println(s);
}

this is not only for String[] args. for all Iterable ..

You'd like a method that would print every single index of the String array, i.e.

public static void printStringArray(String[] myArray){
    for(int i = 0; i < myArray.length; i++){
        System.out.println(myArray[i]);
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top