How to convert a String to ArrayList? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I store an ArrayList in a file as a String ( arrayList.toString()).
How do i convert it back into an ArrayList?
Overall, I just want to convert something like: [Eve, Anna, Tony, Steve] back to an ArrayList. Any ideas?

All you need is a java.lang.String's split() & java.util.Arrays asList():
String fruits = "Orange,Apple,Grape";
String[] array = fruits.split(",");
List<String> list = Arrays.asList(array);
//Code to check the content in the List (for your understanding only)
for(String s : list) {
System.out.println(s);
}
Prints below output:
Orange
Apple
Grape

Related

Merge an List of Object and an Object to a new array in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have List<BigInteger> sample1 = Lists.newArrayList(BIGINTEGER1, BIGINTEGER2,BIGINTEGER3);
I have to create new List sample2 with one more value BIGINTEGER4 along with sample1 list:
How can I do that?
You can create a new ArrayList, and pass the other list in the constructor - the contents from the other list will then be passed/copied into the new:
List<BigInteger> sample2 = new ArrayList<>(sample1);
sample2.add(BIGINTEGER4);

How to map an arraylist in one line in java or kotlin? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How to write the following code in one line in the function argument (in Kotlin or Java)?
/* list: ArrayList<String>, map: Map<String, String> */
val newList = ArrayList<String>()
for (item in list) newList.add(map[item])
someFun(newList)
I expect something like this:
someFun(/* code */)
Here is for Kotlin:
someFun(list.mapNotNull { map[it] })

Find the specific word in the text file and to store that word in array list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to find the specific word in the text file and store that word in the array of list.
I do not how it can be achieved or approached.
A basic solution:
public ArrayList getWords(String searchWord, String listOfWords){
ArrayList<String> foundWords = new ArrayList<>();
if (listOfWords.contains(searchWord)){
foundWords.add(searchWord);
}
return foundWords;
}

Java mismatch between String and String[] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm using Java to implement the Apriori algorithm but one problem exists.
ArrayList<String> l1=new ArrayList<>();//L1
...
ArrayList<String[]> lk1=l1;//Lk-1
Then it warns that: cannot convert from ArrayList<String> to ArrayList<String[]>. How can I solve this problem?
They're different types. Ignore the collection and just look at the underlying type.
String str = "This is a string.";
String[] array = str;
These are two different types, unconvertable, so as T for the collection, the collections are then unconvertable. Very basic concept here.

java string add comma become array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
String items = "bookcupdoll";
I want to add a comma in items variable like below:
book,cup,doll
How should I make the string become an array?
If you are asking how to convert a string to an array...
There is a handy method called "split(String separator)"
If the string looks like : String items = "book,cup,doll";
you can use it like this
String[] array = items.split(",");
And you will end up with an array of size 3 with the 3 different elements.

Categories

Resources