C# => JAVA: Filling a static ArrayList on declaration. Possible?
Solution 1
I don't understand what you mean by
able to access the values without creating a instance of the class
but the following snippet of code in Java has pretty much the same effect in Java as yours:
private static List<String> inputs = Arrays.asList("Foo", "Bar", "Foo2", "Bar2");
Solution 2
You can use Double Brace Initialization. It looks like:
private static List<String> inputs = new ArrayList<String>()
{{ add("Foo");
add("Bar");
add("Foo2");
add("Bar2");
}};
Solution 3
You can make static calls by enclosing them within static{} brackets like such:
private static final List<String> inputs = new ArrayList<String>();
static {
inputs.add("Foo");
inputs.add("Bar");
inputs.add("Foo2");
inputs.add("Bar2");
}
Solution 4
Do you need this to be an ArrayList
specifically, or just a list?
Former:
private static java.util.List<String> inputs = new java.util.ArrayList<String>(
java.util.Arrays.<String>asList("Foo", "Bar", "Foo2", "Bar2"));
Latter:
private static java.util.List<String> inputs =
java.util.Arrays.<String>asList("Foo", "Bar", "Foo2", "Bar2");
java.util.Arrays#asList(...) API
Solution 5
You may enjoy ImmutableList
from Guava:
ImmutableList<String> inputs = ImmutableList.of("Foo", "Bar", "Foo2", "Bar2");
The first half of this youtube video discusses the immutable collections in great detail.
Peterdk
Indie developer of mobile apps at Umito. Android apps are main focus. Top apps are: Mini Piano Lite (1.4 million+), Fretter and KeyChord. The latter one has been featured on Google Play. Also co-dev of Addons Detector (1 million+) Experience: Java, Swift, Ruby, Objective-C, C# Learning: Kotlin and Rust. Minor experience: C, C++
Updated on June 25, 2022Comments
-
Peterdk almost 2 years
In my C# project I have a static List that gets filled immediately when declared.
private static List<String> inputs = new List<String>() { "Foo", "Bar", "Foo2", "Bar2"};
How would I do this in Java using the ArrayList?
I need to be able to access the values without creating a instance of the class. Is it possible?
-
Brett over 14 yearsAs it is a static field, it really should be immutable. Unfortunately this adds to the verbosity.
private static final List<String> inputs = Collections.unmodifiableList(Arrays.asList("Foo", "Bar", "Foo2", "Bar2"));
. Should get list literals in JDK7. -
jdmichal over 14 yearsYay for the less verbose, later answer winning out? (No offense to you MAK. Just stating my perceived truth.)
-
Adriaan Koster over 14 yearsIMO This is the best answer, since it doesn't rely on helper class java.util.Arrays.
-
jdmichal over 14 yearsBut it does create a somewhat hidden anonymous inner class. Subjective call on whether or not that is a better tradeoff.
-
MAK over 14 years@jdmichal: I think your answer was posted while I was still writing mine - otherwise I wouldn't have bothered to say the same thing again. I guess the slowest gun won :).
-
Peterdk over 14 yearsThat's nice, I didn't know that.
-
Leo Izen over 10 yearsI often like to run
new ArrayList<>(Arrays.asList(...))
becauseArrays.asList()
doesn't produce a full implementation ofList
butArrayList
does. It's slightly slower if this has to be done repeatedly but if it's a one-shot deal then it's fine.