Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7836435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:08:49+00:00 2026-06-02T14:08:49+00:00

I’m working on something related to generics in java and I’ve found this example

  • 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
        } 
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:08:57+00:00Added an answer on June 2, 2026 at 2:08 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.