This question already has answers here:
Convert String to int array in java
(11 answers)
Closed 5 years ago.
I've been trying to convert a String like this: "[1,2,3]" to an ArrayList in Java.
What I tried so far is to use GSon library to convert the String mentioned above to List using:
Gson - convert from Json to a typed ArrayList<T>
Convert Gson Array to Arraylist
Parsing JSON array into java.util.List with Gson
but it ends with an exception (I can provide more details if needed). The first question should be actually if it's a good approach to achieve such a transformation?
A non-regex method would be to remove the brackets from the string and then split on the commas. Then convert to an ArrayList.
String s = "[1, 2, 3]";
String[] splits = s.replace("[","").replace("]","").split(",");
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(splits));
I suggest you use regular expression to convert the string from "[1,2,3,...]" to "1,2,3,..." and then ue asList() method of Arrays class to convert it to a List.
Refer the following snippet
import java.util.regex.*;
...
...
String str = "[1,2,3,4,5,6,7,8,9,10]";
str = str.replaceAll("[(*)])","$1");
List<String> items = Arrays.asList(str.split("\\s*,\\s*"));
Related
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.
I have the following string and I need to split it to get the two objects inside:
[Object{value1="1", value2="2"}, Object{Value1="1", value2="2"}]
You could try:
String[] splitTextObject = YOUR_STRING.split(", ");
String object1 = splitTextObject[0];
String object2 = splitTextObject[1];
...
But I don't think you actually need to split the string this way in order to achieve getting each object, and instead you should consider parsing your JSON. Perhaps utilise GSON.
i have ArrayList with multiple value. i want to convert this ArrayList into String to save in sharedPreferences, then I want to retrieve the String and convert it back to ArrayList
Please tell how to do that? (or any other idea to store and retrieve ArrayList)
Convert arraylist to string:
String str = "";
for (String s : arraylist)
{
str += s + ",";
}
Save string into sharedpreference:
PreferenceManager.getDefaultSharedPreferences(context).edit().putString("mystr", str).commit();
get string from sharedpreference:
String str = PreferenceManager.getDefaultSharedPreferences(context).getString("mystr", "defaultStringIfNothingFound");
Convert string to arraylist:
List<String> arraylist = new ArrayList<String>(Arrays.asList(str.split(",")));
You can use Google's GSON.
Gson is a Java library that can be used to convert Java Objects into
their JSON representation. It can also be used to convert a JSON
string to an equivalent Java object. Gson can work with arbitrary Java
objects including pre-existing objects that you do not have
source-code of.
http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html
https://code.google.com/p/google-gson/
You get the idea:
Store: Convert Object to JSON String -> Save string
Retrieve: Get string -> Convert from JSON to Object
This question already has answers here:
Convert ArrayList<String> to String[] array [duplicate]
(6 answers)
Convert list to array in Java [duplicate]
(11 answers)
Closed 9 years ago.
List<String> list = getNames();//this returns a list of names(String).
String[] names = (String[]) list.toArray(); // throws class cast exception.
I don't understand why ? Any solution, explanation is appreciated.
This is because the parameterless toArray produces an array of Objects. You need to call the overload which takes the output array as the parameter, and pass an array of Strings, like this:
String[] names = (String[]) list.toArray(new String[list.size()]);
In Java 5 or newer you can drop the cast.
String[] names = list.toArray(new String[list.size()]);
You are attempting to cast from a class of Object[]. The class itself is an array of type Object. You would have to cast individually, one-by-one, adding the elements to a new array.
Or you could use the method already implemented for that, by doing this:
list.toArray(new String[list.size()]);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert a generic list to an array
So I am trying to convert the contents of my arraylist into an array. However I keep getting the error
Type mismatch: cannot convert from Object[] to String
or the error
Type mismatch: cannot convert from String[] to String
any ideas how to solve this, I'm drawing up blanks. Thanks
Here is one way:
String[] listArr= new String[yourList.size()];
Iterator<String> listIter = yourList.iterator();
while (listIter .hasNext()) {
listArr[count] = listIter .next();
}
Note: There may be syntax errors, I just typed code here.
Try this:
String[] array = arrayList.toArray(new String[0]);
That is, assuming that the ArrayList was declared with type ArrayList<String>. Replace with the appropriate types if necessary.
Another way:
String[] result = new String[arrayList.size()];
arrayList.ToArray( result );