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 3783256
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:11:56+00:00 2026-05-19T11:11:56+00:00

C# lets me make arrays on the fly when I need to pass them

  • 0

C# lets me make arrays on the fly when I need to pass them into functions. Let’s say I have a method called findMiddleItem(String[] items). In C#, I can write code like:

findMiddleItem(new String[] { "one", "two", "three" });

It’s awesome, because it means I don’t have to write:

IList<String> strings = new List<String>();
strings.add("one");
strings.add("two");
strings.add("three");
findMiddleItem(strings.ToArray());

Which sucks, because I don’t really care about strings — it’s just a construct to let me pass a string array into a method that requires it. A method which I can’t modify.

So how do you do this in Java? I need to know this for array types (eg. String[]) but also generic types (eg. List).

  • 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-05-19T11:11:56+00:00Added an answer on May 19, 2026 at 11:11 am

    A List and an Array are fundamentally different things.

    A List is a Collection type, an implementation of an interface.
    An Array is a special operating system specific data structure that can only be created through either a special syntax or native code.

    Arrays

    In Java, the array syntax is identical to the one you are describing:

    String[] array = new String[] { "one", "two", "three" };
    

    Reference: Java tutorial > Arrays

    Lists

    The easiest way to create a List is this:

    List<String> list = Arrays.asList("one", "two", "three");
    

    However, the resulting list will be immutable (or at least it won’t support add() or remove()), so you can wrap the call with an ArrayList constructor call:

    new ArrayList<String>(Arrays.asList("one", "two", "three"));
    

    As Jon Skeet says, it’s prettier with Guava, there you can do:

    Lists.newArrayList("one", "two", "three");
    

    Reference: Java Tutorial > The List Interface, Lists (guava javadocs)

    VarArgs

    About this comment:

    It would be nice if we would be able to do findMiddleItem({ "one", "two", "three" });

    Java varargs gives you an even better deal:

    public void findMiddleItem(String ... args){
        //
    }
    

    you can call this using a variable number of arguments:

    findMiddleItem("one");
    findMiddleItem("one", "two");
    findMiddleItem("one", "two", "three");
    

    Or with an array:

    findMiddleItem(new String[]{"one", "two", "three"});
    

    Reference: Java Tutorial > Arbitrary Number of Arguments

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

Sidebar

Related Questions

Let's say I have two arrays: int[] array1 = new int[2000000]; int[] array2 =
Let's say I have these two arrays: var array1 = new[] {"A", "B", "C"};
I have four arrays that are coming in from the client. Let's say that
Lets say I have a file called foo.txt encoded in utf8: aoeu qjkx ñpyf
I have to make a two-dimensional array based on the number of lets say
Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
Let's say I wanted to make a python script interface with a site like
Ok, let's see if I can make this make sense. I have a program
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Lets say that you have websites www.xyz.com and www.abc.com. Lets say that a user

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.