Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

can I make this two method into one method?

Thread view: 
jtl.zheng - 24 Jul 2006 13:18 GMT
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
: )
Bart Cremers - 24 Jul 2006 13:46 GMT
jtl.zheng schreef:

> codes
> ---------------------------
[quoted text clipped - 19 lines]
> 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
Simon - 24 Jul 2006 14:22 GMT
jtl.zheng schrieb:
> codes
> ---------------------------
[quoted text clipped - 10 lines]
> }
> --------------------------

At least for Object arrays you can also use generics:

   public static <T> int getArrayLength(T[] t) {
    return t.length;
   }
jtl.zheng - 24 Jul 2006 14:54 GMT
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[i] + "  ");
     }
   }
}

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[i] + "  ");
     }
   }
}
------------------------

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

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

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
: )
Simon - 24 Jul 2006 15:18 GMT
> in Bart Cremers's
> I can't perform System.out.print(array[i]);

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.
jtl.zheng - 25 Jul 2006 03:38 GMT
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();
   }
 }
-------------------------


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.