0

I'm working on something related to generics in java and I've found this example on the internet here. This might be a simple question but I want to find a correct answer from someone who worked with generics. This generic method takes as input argument different types of arrays and displays them. Now I want to do this by reading those arrays from command line. Can you please help me find the correct way to do it? Thanks.

public class GenericMethodTest
    {
       // generic method printArray                         
       public static < E > void printArray( E[] inputArray )
       {
          // Display array elements              
             for ( E element : inputArray ){        
                System.out.printf( "%s ", element );
             }
             System.out.println();
        }

        public static void main( String args[] )
        {
            // Create arrays of Integer, Double and Character
            Integer[] intArray = { 1, 2, 3, 4, 5 };
            Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
            Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };

            System.out.println( "Array integerArray contains:" );
            printArray( intArray  ); // pass an Integer array

            System.out.println( "\nArray doubleArray contains:" );
            printArray( doubleArray ); // pass a Double array

            System.out.println( "\nArray characterArray contains:" );
            printArray( charArray ); // pass a Character array
        } 
    }
3
  • I'm not sure exactly what you're asking here - is it just "how do I convert the array of Strings from command-line args into an array of .e.g. ints"? Commented Apr 25, 2012 at 8:56
  • You want to read the elements of the array through command prompt? And you know how to run the program using command prompt? Commented Apr 25, 2012 at 8:58
  • @Doyle ints or double or float or whatever just as you call the method with different arrays i want to input the elements myself and then print them back (I know it's a simple example and stupid ) instead of defining them as Integer[] bla= {1, 2, 3}. Then You get a String right? I can make an array of strings and then display them but I would like my method to be called with different arrays not just string. Commented Apr 25, 2012 at 9:53

1 Answer 1

2

I think you will have to include the datatype as part of your command line, or else have a routine which detects the type by heuristics. Something like

java GenericMethodTest "doubleArray" "1.1 2.2"

where you have two args, the type and the array. Then you will need a constructor which converts the second argument from a string to an array, such as

public DoubleArray (String sin) {
    String[] ss = sin.split(" ")'
    double dd[] = new Double[ss.length];
    for (String s : ss) {
        dd[i++] = new Double(s);
    }
}

I do this sort of thing a lot in scientific programming and I usually end up with special cases for the datatypes. How does the program know what is an integer or a string or a double? You either have to tell it or you have to give it a chance to guess.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your reply. I upvoted your answer but it is not really what I was looking for. Maybe my question is a little bit stupid. I actually have a sort method which is generic and I want to sort any type of data I input. As you said the idea is to split the string and construct a Double array. But you don't know what type of data you input. I thought it was something simple enough but I can seem to find the right way to do it. Or maybe I don't have enough experience. Anyway thank you.
@Michiru - this seems to be the correct answer. The arguments will always be strings; you either have to guess what their type is somehow (which is hard and error-prone), or have the user declare their type (such as the "doubleArray" descriptor here). Then you need to define how to convert from a string to any of these types, and call the correct method. I don't know what you had in mind, but I can't think of any feasible solution that wouldn't involve these two steps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.