Java equivalent of anonymous C # arrays and lists?

C # allows me to create arrays on the fly when I need to pass them to functions. Say I have a method called findMiddleItem(String[] items) . In C #, I can write code like:

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

This is awesome because it means I don't need to write:

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

Which sucks because I really don't care about strings - it's just a construct that allows me to pass a string array to a method that requires it. A method that I cannot change.

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

+6
java collections arrays c #
source share
6 answers

A list and an array are fundamentally different things.

A List is a Collection , an implementation of the interface.
An array is a special data structure of a specific operating system, which can be created only through special syntax or native code.

Arrays

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

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

Link: Java Tutorial> Arrays

Lists

The easiest way to create a list:

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

However, the resulting list will be immutable (or at least it will not support add() or remove() ), so you can wrap the call with a call to the ArrayList constructor:

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

As John Skeet says, he is prettier than Guava, there you can do:

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

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

with a variable number of arguments

About this comment:

It would be nice if we could find 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"}); 

Link: Java Tutorial > Arbitrary Number of Arguments

+17
source share

You can do it the exact same way:

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

valid in Java. Assuming findMiddleItem is defined as:

 findMiddleItem(String[] array) 
+3
source share

In Java, you can build an array in the same way:

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

You cannot build a List<T> exactly the same way, but there are different ways around this, for example. wrapping an array or using some Guava Lists.* methods. (Your code trying to call findMiddleItem with an argument of type IList<string> would not have been compiled since IList<string> not necessarily string[] .) For example, if findMiddleItem really had a parameter of type List<String> you can use:

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

As with no collection initializers (or object initializers), Java also does not have implicitly typed arrays ... your C # source code can be condensed in C # 3 and higher:

 findMiddleItem(new[] { "one", "two", "three" }); 
+3
source share

I think this is the same syntax in Java. It works:

 public class Main { public static void main( String[] args ) { method1( new String[] {"this", "is", "a", "test"} ); } private static void method1( String[] params ) { for( String string : params ) System.out.println( string ); } } 

I think this will work on non-static methods as well.

+1
source share

Same as in C # findMiddleItem(new String[] { "one", "two", "three" }) ;

In addition, for future reference, you can build the list in Java in a slightly less verbal form * :

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

* As Sean pointed out, this can be considered bad practice as it creates an anonymous subclass of ArrayList .

0
source share

Besides using vargs like

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

You can do

 findMiddleItem("one,two,three,four,five".split(",")); 

EDIT: to turn a String into a List, you can use a helper method.

 public static List<String> list(String text) { return Arrays.asList(text.split(",")); } findMiddleItem(list("one,two,three,four,five")); 
0
source share

All Articles