can I make this two method into one method?

Discussion in 'Java' started by jtl.zheng, Jul 24, 2006.

  1. jtl.zheng

    jtl.zheng Guest

    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
    : )
     
    jtl.zheng, Jul 24, 2006
    #1
    1. Advertising

  2. jtl.zheng

    Bart Cremers Guest

    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
     
    Bart Cremers, Jul 24, 2006
    #2
    1. Advertising

  3. jtl.zheng

    Simon Guest

    jtl.zheng schrieb:
    > 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;
    }
     
    Simon, Jul 24, 2006
    #3
  4. jtl.zheng

    jtl.zheng Guest

    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
    : )
     
    jtl.zheng, Jul 24, 2006
    #4
  5. jtl.zheng

    Simon Guest

    jtl.zheng wrote:
    > 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.
     
    Simon, Jul 24, 2006
    #5
  6. jtl.zheng

    jtl.zheng Guest

    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();
    }
    }
    -------------------------
     
    jtl.zheng, Jul 25, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ford Prefect alias Armin

    Can't make two request at once, one blocks all.....

    Ford Prefect alias Armin, Dec 3, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    331
    Ford Prefect alias Armin
    Dec 3, 2003
  2. Randall Parker
    Replies:
    0
    Views:
    1,645
    Randall Parker
    Dec 4, 2005
  3. Oxnard
    Replies:
    4
    Views:
    372
    Joona I Palaste
    Oct 26, 2004
  4. news.frontiernet.net
    Replies:
    6
    Views:
    1,153
    news.frontiernet.net
    Apr 16, 2004
  5. Tobias Müller
    Replies:
    0
    Views:
    122
    Tobias Müller
    Dec 9, 2013
Loading...

Share This Page