Putting arrays in ArrayList [duplicate] - java

This question already has answers here:
Create ArrayList from array
(42 answers)
Closed 8 years ago.
Probably a very stupid question, but how can I put an array into an ArrayList?
Example:
String[] strings = {"ex","okay",};
ArrayList<String> stringslist = new ArrayList<String>();
Now how do I put everything in strings, into stringlist?

You need to use Arrays.asList() function.
String[] strings = {"ex","okay",};
ArrayList newStringslist = new ArrayList<Element>(Arrays.asList(strings))
Tutorials-Point Link
Also - Create ArrayList from array

new ArrayList<Element>(Arrays.asList(array)) -- JAVA
per answer at Create ArrayList from array

Related

Way to create a list in Java [duplicate]

This question already has answers here:
What does it mean to program to an interface?
(17 answers)
Arraylist and List in Java
(3 answers)
Closed 7 months ago.
I am wondering about the difference between these two options to create a list of string. Which one to use when creating a list in Java?
Option 1
List<String> list1 = new ArrayList<String>();
Option 2
ArrayList<String> list2 = new ArrayList<String>();

pick first N elements from a static list java [duplicate]

This question already has answers here:
Get only part of an Array in Java? [duplicate]
(8 answers)
Fastest way to get the first n elements of a List into an Array
(6 answers)
Closed 4 years ago.
I want to pick first N elements from a list in java
String[] ligature = new String{there,is not,a,single,person,who,knows,the,answer};
Now I want to pick first 5 elements from this.something like
Stiring[] selectedLigature = ligature[0:4];
I want to do it without using for loop.
Do not stream this simple case, there is subList for this:
// for a List
yourList.subList(0, 5)...
// for an array
Arrays.copyOfRange
Arrays class has a method for this:
Arrays.copyOfRange(ligature, 0, 5);

Java sorting ArrayList of String Array [duplicate]

This question already has answers here:
Sorting an ArrayList of objects using a custom sorting order
(12 answers)
Closed 4 years ago.
I have an ArrayList in the form ArrayList<String[]> table_list = new ArrayList<String[]>();
So for example table_list.get(0) would look like {"Title", "Yes", "No"}.
Now let's say I have a lot of titles and want tot sort those titles in my table alphabetically, what do I do? Collections.sort() does not seem to work.
Thanks.
pass a custom comparator:
table_list.sort(Comparator.comparing(e -> e[0]));

What are the <> for in an ArrayList, List or other types that use <>? [duplicate]

This question already has answers here:
What is the point of the diamond operator (<>) in Java?
(7 answers)
Closed 7 years ago.
I've Googled around but I can't find the use for the <> in ArrayList or List. I know how to use ArrayList but I like to know how things work as well. What are the <> for? I figure it's like an argument of sorts since it specifies the type but it's not an argument since it doesn't go in () so what do the <> mean?
to specify the generic type.
List<String> strings = new ArrayList<String>(); will restrict only strings to be added
When you say List strings = new ArrayList(); you are not specifying which kind of object the strings list accepts. So, strings.add(new Object()) is also possible. By specifying List<String> strings = new ArrayList<String>(); you restrict the arraylist to access only String elements.

Removes and returns the first occurrence of the specified string in array using Java [duplicate]

This question already has answers here:
Remove a specific string from an array of string
(5 answers)
Closed 8 years ago.
I am trying to delete from an array the first occurence, not all elements like the searched element The array is something like:
String[] names = {"Becky", "Rosa", "Tina", "Jill", "Rosa", "Bill"};
And I want to be able to say remove(Rosa) and only find and remove the first element in the array named Rosa.
So you might want to consider using an Arraylist for dynamic removal and resizing
import java.util.ArrayList;
ArrayList<string> persons = new ArrayList<string>();
persons.add("Becky");
//... adding in people to the list
//then if you want to remove someone name "becky"
persons.removeall.(collection.singleton("Becky"))

Categories