This question already has answers here:
java vector to arraylist
(3 answers)
Closed 5 years ago.
Can somebody show the correct syntax to replace a vector with an ArrayList?
Original code -
StringBuilder msg = new StringBuilder();
msg.append(event.toString());
Vector<? extends VariableBinding> varBinds = event.getPDU()
.getVariableBindings();
Have tried -
List<String> variables = new ArrayList<>();
for (VariableBinding binding : event.getPDU().getVariableBindings()) {
variables.add(String.valueOf(binding.getVariable()));
}
But multiple issues (diamond operator not supported, can convert ArrayList to String). Netbeans / JDK1.6
And tried -
ArrayList<String> list = new ArrayList<String>(varBinds) = event.getPDU().getVariableBindings();
But unexpected type, required variable found value, cannot find symbol variable varBinds.
Thoughts appreciated
Regards
Active
The diamond operator was not available in JDK 1.6. You need to define the ArrayList using the old style generics form:
import java.util.List;
import java.util.ArrayList;
List<String> variables = new ArrayList<String>();
Following the docs of SNMP4J at
http://www.snmp4j.org/doc/index.html
The event.getPDU().getVariableBindings() will return an Vector of VariableBindings
If you want to store that in an ArrayList of VariableBindings you could do
List<VariableBinding> variables = new ArrayList<String>(event.getPDU().getVariableBindings());
However if you are looking to store a string representation of the VariableBinding you can retrieve the string representation of the underlying Variable
e.g.
List<String> variables = new ArrayList<String>();
for (VariableBinding binding : event.getPDU().getVariableBindings()) {
variables.add(binding.getVariable().toString());
}
It all depends on what you're wanting to achieve.
Related
This question already has answers here:
Creating prepopulated set in Java [duplicate]
(6 answers)
How to initialize HashSet values by construction?
(24 answers)
Closed 4 years ago.
How to create checked Set with one (or more elements)
I have some Objects of type A and I want to create checked Set with all these objects
A element1; //not null
A element2; //not null; optional
my solution:
Set<A> s = new HashSet<>(1);
a.add(element1);
a.add(element2); //optional
Q: Is there any standard util class to create it in simple way?
Something like
List<A> l = java.util.Arrays.asList(element1);
List<A> l = java.util.Arrays.asList(element1, element2); // optional
but for Set
In JDK-8:
Stream.of(element1, element2).collect(toSet());
or to guarantee the type of set returned:
Stream.of(element1, element2).collect(toCollection(HashSet::new));
if not on JDK8 yet then the best option is probably:
new HashSet<>(Arrays.asList(element1, element2));
Set<A> s = new HashSet<>(Arrays.asList(element1, element2));
This question already has answers here:
Generic array creation error
(5 answers)
How to create a generic array in Java?
(32 answers)
Closed 7 years ago.
I get the following error in my IDE "generic array creation"
I googled it but found very long explanations and didn't quite understand what the best solution is to this problem.
If anyone could suggest the best solution to this to get my code to compile...
public ArrayList<String>[] getClosedTicketIDs(Account account) {
ArrayList<String> closedSourceTickets = new ArrayList<>();
ArrayList<String> closedAccountTickets = new ArrayList<>();
// ...some unimportant to this example code...
// return
ArrayList<String>[] a = new ArrayList<String>[2]; // <-- generic array creation error
a[0] = closedSourceTickets;
a[1] = closedAccountTickets;
return a;
}
My objective is to return an array consisting of 2 ArrayList<String> (no more, no less).
You can only create raw array types. You need to do this: a = new ArrayList[2];
You cant do that but you can do
List<List<String>> a=new ArrayList<ArrayList<String>>();
but the better would be
ArrayList[] a=new ArrayList[n];
as you can fix the size in this.
This question already has answers here:
make arrayList.toArray() return more specific types
(6 answers)
Closed 7 years ago.
If let say i have;
ArrayList <Double> myV = new ArrayList <Double>();
myV.add(12.2);
myV.add(3.2);
myV.add(5.00);
// this is error
Number[] youV = myV.toArray();
the below code is error when I compiled it. What should I do then to convert the ArrayList into Number of arrays type?
How to convert them into Number[] ?
And lastly, is this code list safe for us to use, if I apply this code
inside Android?
This code should do what you need.
Number[] result = new Number[myV.size()];
myV.toArray(result);
To answer your first question, You can convert them like this.
public static void main(String[] args) {
ArrayList<Double> myV = new ArrayList<Double>();
myV.add(12.2);
myV.add(3.2);
myV.add(5.00);
Number[] target = new Number[myV.size()];
myV.toArray(target);
System.out.println(target[0]);
}
I'm currently studying for the Java OCA exam and came across a question relating to ArrayList declarations.
Which of the following is valid?:
1. ArrayList al1 = new ArrayList();
2. ArrayList al2 = new ArrayList<>();
3. ArrayList<> al3 = new ArrayList<>();
4. ArrayList<Double> al4 = new ArrayList<>();
5. ArrayList<Double> al5 = new ArrayList<Float>();
According to my book, answers 1,2 and 4 are valid. Answers 3 and 5 are invalid.
However, no proper explanation is given. All it does is show the standard way to declare an ArrayList:
ArrayList<E> al3 = new ArrayList<E>();
and mentions that it's also valid to declare the ArrayList without the generic part.
I'm also unable to find a decent article on this topic online. Can someone explain (or point me in the direction of a good article) the different permutations above?
Thanks in advance.
1 is valid in all versions of Java
Here you are declaring the ArrayList without using Generics. This means regardless of what you add to the arraylist, when you get it back out it will be of type Object and will require casting to a type. This is the old way of using Collections in Java 1.4 (pre generics) and is supported for backwards compatibility. Nowadays you should always use Generics.
2 and 4 are valid in Java 7 only
The empty brackets: <> are Java7's new Type inference that means you don't have to specify the type twice. Note Java7, it wont work in older versions.
So in Java7
ArrayList<Double> al4 = new ArrayList<>();
Is the same as
ArrayList<Double> al4 = new ArrayList<Double>();
This link has more info on type inference: http://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html
3 is invalid because if you are using Generics you must specify a type in the variable declaration.
5 is invalid because if you declare a List with type Double you cannot then assign it a List that is of Type Float, you can only assign it a list of type Double.
So this would be valid:
ArrayList<Double> al5 = new ArrayList<Double>();
Below 2 will be valid only in Java SE7. Java SE7 allows type inference so you don't need to provide type inside <>again.
ArrayList<Double> al4 = new ArrayList<>();
ArrayList al2 = new ArrayList<>();
On the other hand below one is valid on all Java versions; this is to ensure backward compatibility of non-generic code.
ArrayList al1 = new ArrayList();
Below is not allowed as Collection of Float is not a sub type of collection of Double. Moreover Float is not subtype of Double; so no question of it being a valid declaration.
Note that even array version doesn't compile.
Double[] dd = new Float[5]; //won't compile
ArrayList<Double> al5 = new ArrayList<Float>(); //won't compile
Below one is not a valid declaration.
ArrayList<> al3 = new ArrayList<>();
This question already has answers here:
How can I create an Array of ArrayLists?
(20 answers)
Closed 9 years ago.
How can I initialize an Array of ArrayList<String>?
I tried this syntax but it didn't work:
ArrayList<String>[] subsection = new ArrayList<String>[4];
you can define like this :
ArrayList<String>[] lists = (ArrayList<String>[])new ArrayList[10];
lists[0] = new ArrayList<String>();
lists[0].add("Hello");
lists[0].add("World");
String str1 = lists[0].get(0);
String str2 = lists[0].get(1);
System.out.println(str1 + " " + str2);
That syntax works fine for the non-generic ArrayList. (ideone)
But it won't work for the generic ArrayList<E>: (ideone)
This code:
ArrayList<String>[] subsection = new ArrayList<String>[4];
Gives a compiler error:
Main.java:8: generic array creation
ArrayList<String>[] subsection = new ArrayList<String>[4];
For the generic version use an ArrayList<ArrayList<E>>:
ArrayList<ArrayList<String>> subsection = new ArrayList<ArrayList<String>>();
Okay after comment, I thought well... your right why not.
Figured it out.
ArrayList[] test = new ArrayList[4];
test[3] = new ArrayList<String>();
test[3].add("HI");
System.out.println(test[3].get(0));
Though I will be honest, I am not really sure WHY this works.
Once you assign the first item of test as a new Collection, it will only allow all other items in the array to be that type. So you couldn't do
test[3] = new ArrayList<String>();
test[2] = new HashSet<String>();
Look into generics as type clarification process, you can assign typed value to a variable of raw type AND vice versa. In core generics are a shortcut for the programmers to avoid making type casting too much, which also helps to catch some logical errors at compile time.
At the very basics ArrayList will always implicitly have items of type Object.
So
test[i] = new ArrayList<String>(); because test[i] has type of ArrayList.
The bit
test[3] = new ArrayList<String>();
test[2] = new HashSet<String>();
did not work - as was expected, because HashSet simply is not a subclass of ArrayList. Generics has nothing to do here. Strip away the generics and you'll see the obvious reason.
However,
test[2] = new ArrayList<String>();
test[3] = new ArrayList<HashSet>();
will work nicely, because both items are ArrayLists.
Hope this made sense...