K
KevinSimonson
Java lets one assign a literal array to a variable, and then pass the
variable
to a method. Why can't one just pass the literal array to the
method? For
example, I can write:
public class Bug
{
private static void processArray ( int number
, String[] strngArray)
{
System.out.println( "Number is " + number + '.');
for (int index = 0; index < strngArray.length; index++)
{ System.out.println
( "Array position " + index + " is \"" + strngArray[ index] +
"\".");
}
}
public static void main ( String[] arguments)
{
String[] sa = { "Abc", "Def" };
processArray( 7, sa);
}
}
and have my code work just fine, but if I try:
public class Bug
{
private static void processArray ( int number
, String[] strngArray)
{
System.out.println( "Number is " + number + '.');
for (int index = 0; index < strngArray.length; index++)
{ System.out.println
( "Array position " + index + " is \"" + strngArray[ index] +
"\".");
}
}
public static void main ( String[] arguments)
{
processArray( 7, { "Abc", "Def" });
}
}
and then compile it I get error messages:
sh-4.1$ javac Bug.java
Bug.java:15: illegal start of expression
processArray( 7, { "Abc", "Def" });
^
Bug.java:15: ';' expected
processArray( 7, { "Abc", "Def" });
^
Bug.java:15: illegal start of expression
processArray( 7, { "Abc", "Def" });
^
Bug.java:15: ';' expected
processArray( 7, { "Abc", "Def" });
^
Bug.java:15: illegal start of type
processArray( 7, { "Abc", "Def" });
^
Bug.java:17: class, interface, or enum expected
}
^
6 errors
sh-4.1$
Anybody know why this happens? Is there any way to pass a literal
array to a
method?
Kevin Simonson
variable
to a method. Why can't one just pass the literal array to the
method? For
example, I can write:
public class Bug
{
private static void processArray ( int number
, String[] strngArray)
{
System.out.println( "Number is " + number + '.');
for (int index = 0; index < strngArray.length; index++)
{ System.out.println
( "Array position " + index + " is \"" + strngArray[ index] +
"\".");
}
}
public static void main ( String[] arguments)
{
String[] sa = { "Abc", "Def" };
processArray( 7, sa);
}
}
and have my code work just fine, but if I try:
public class Bug
{
private static void processArray ( int number
, String[] strngArray)
{
System.out.println( "Number is " + number + '.');
for (int index = 0; index < strngArray.length; index++)
{ System.out.println
( "Array position " + index + " is \"" + strngArray[ index] +
"\".");
}
}
public static void main ( String[] arguments)
{
processArray( 7, { "Abc", "Def" });
}
}
and then compile it I get error messages:
sh-4.1$ javac Bug.java
Bug.java:15: illegal start of expression
processArray( 7, { "Abc", "Def" });
^
Bug.java:15: ';' expected
processArray( 7, { "Abc", "Def" });
^
Bug.java:15: illegal start of expression
processArray( 7, { "Abc", "Def" });
^
Bug.java:15: ';' expected
processArray( 7, { "Abc", "Def" });
^
Bug.java:15: illegal start of type
processArray( 7, { "Abc", "Def" });
^
Bug.java:17: class, interface, or enum expected
}
^
6 errors
sh-4.1$
Anybody know why this happens? Is there any way to pass a literal
array to a
method?
Kevin Simonson