-1

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

2
  • 2
    This seems like a good opportunity to upgrade to a supported Java version. Commented Dec 7, 2017 at 9:12
  • Hello Shivam, tried your ArrayList (see my original post) but multiple issues, probably syntax on my part Commented Dec 7, 2017 at 9:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

cannot find symbol classList
You need to import java.util.List and ArrayList. Will edit Answer
Thanks Spangen, can you show me the entire command including the getPDU part?
What is the full type of VariableBinding including its package? For instance is it a org.snmp4j.smi.VariableBinding ?
yes, snmp4j, returns multiple variables
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.