Question: 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).

Answer: 
A List and an Array are fundamentally different things.

A [`List`](http://download.oracle.com/javase/6/docs/api/java/util/List.html) is a [`Collection`](http://download.oracle.com/javase/6/docs/api/java/util/Collection.html) 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](http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html)

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`](http://download.oracle.com/javase/tutorial/collections/interfaces/list.html), [`Lists` (guava javadocs)](http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Lists.html)

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`](http://download.oracle.com/javase/tutorial/java/javaOO/arguments.html#varargs)
The same way as in C# `findMiddleItem(new String[] { "one", "two", "three" })`;

Also, for future reference, you can construct a List in Java in a slightly less-verbose way\*:

```
List<String> myStringList = new ArrayList<String>() {{
   add("a");
   add("b");
   add("c");
}};

```

\* As Sean pointed out, this can be considered bad practice since it does create an anonymous subclass of `ArrayList`.