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).
C is a middle-level language as it binds the bridges between machine-level and high-level languages. Java is a high-level language as the translation of Java code takes place into machine language, using a compiler or interpreter. C is only compiled and not interpreted. Java is both compiled and interpreted.
The very first Java compiler was developed by Sun Microsystems and was written in C using some libraries from C++. Today, the Java compiler is written in Java, while the JRE is written in C.
These two languages are very similar in terms of syntax and language features. They are so similar that if you're shown some portion of C++ code from a project and asked whether it's C++ or Java code, you may confuse yourself.
C is a compiled language that is it converts the code into machine language so that it could be understood by the machine or system. Java is an Interpreted language that is in Java, the code is first transformed into bytecode and that bytecode is then executed by the JVM (Java Virtual Machine).
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.
In Java, the array syntax is identical to the one you are describing:
String[] array = new String[] { "one", "two", "three" };
Reference: Java tutorial > Arrays
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With