can I make this two method into one method?

J

jtl.zheng

codes
---------------------------
method1:

public static int getArrayLength(String[] s){
return s.length;
}

method2:

public static int getArrayLength(char[] s){
return s.length;
}
--------------------------


as you see, they contain the same body codes
so I want to make this two methods into one menthod
then when I want to modify ,I don't need to write twice in two method

or is there any syntx in Java acted like the C++'s inline function?

thank you very much in advance
: )
 
B

Bart Cremers

jtl.zheng schreef:
codes
---------------------------
method1:

public static int getArrayLength(String[] s){
return s.length;
}

method2:

public static int getArrayLength(char[] s){
return s.length;
}
--------------------------


as you see, they contain the same body codes
so I want to make this two methods into one menthod
then when I want to modify ,I don't need to write twice in two method

or is there any syntx in Java acted like the C++'s inline function?

thank you very much in advance
: )

You could go for this, but it can introduce other problems then writing
a similar method eight times to allow all possible array types.

public static int getArrayLength(Object array) {
if (array.getClass().isArray()) {
return Array.getLength(array);
}
throw new IllegalArgumentException("Parameter should be an
array");
}


Regards,

Bart
 
S

Simon

jtl.zheng said:
codes
---------------------------
method1:

public static int getArrayLength(String[] s){
return s.length;
}

method2:

public static int getArrayLength(char[] s){
return s.length;
}
--------------------------

At least for Object arrays you can also use generics:

public static <T> int getArrayLength(T[] t) {
return t.length;
}
 
J

jtl.zheng

Thank you very much
in fact I want to print all the elem in a array
like:

------------------------

public static int printArray(char[] s){
if (s == null) {
System.println("Warming: a null array!!");
}
else {
for (int i = 0; i < s.length; i++) {
System.out.print(s + " ");
}
}
}

public static int printArray(String[] s){
if (s == null) {
System.println("Warming: a null array!!");
}
else {
for (int i = 0; i < s.length; i++) {
System.out.print(s + " ");
}
}
}
------------------------

but your advices can't work out in these codes above

in Bart Cremers's
I can't perform System.out.print(array);

in Simon's the compiler say
"<T>getArrayLength(T[]) cannot be applied to (char[]) "

is there other way?
or is there any syntx in Java acted like the C++'s inline function?

thank you very much
: )
 
S

Simon

jtl.zheng said:
in Bart Cremers's
I can't perform System.out.print(array);


you can use

System.out.println(Array.get(array, i));

If you want that for debugging only, you can also use one of the
Arrays.toString() methods.
 
J

jtl.zheng

haha,it work out now
Thank you very much

now I can print all types of array
my codes is

-------------------------
public static void printArray(Object array) {
if (array == null || !array.getClass().isArray() ) {
System.out.println("Warming: a null array or not a array!!");
}
else {
for (int i = 0; i < Array.getLength(array); i++) {
System.out.print(i + "\t\t");
}
System.out.println();
for (int i = 0; i < Array.getLength(array); i++) {
System.out.print(Array.get(array, 1) + "\t\t");
}
System.out.println();
}
}
-------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top