This question already has answers here:
Create ArrayList from array
(42 answers)
Closed 9 years ago.
I am attempting to change the following array line into an ArrayList that will function the same way:
private String[] books = new String[5];
I changed it to this but it is not functioning properly:
private ArrayList<String>(Arrays.asList(books))
I thought this was how an ArrayList was created
You need to create it like this:
private ArrayList<String> booksList = new ArrayList<String>(Arrays.asList(books));
new ArrayList<String>(Arrays.asList(books)) is the part which is turning your array into an ArrayList.
You could also do:
private List<String> booksList = Arrays.asList(books);
If the fact that it is an ArrayList doesn't matter.
Answered here
new ArrayList<Element>(Arrays.asList(array))
*** new
Related
This question already has answers here:
Syntax for creating a two-dimensional array in Java
(13 answers)
How do I declare and initialize an array in Java?
(31 answers)
Closed 5 years ago.
I want to initialize two dimensional array in constructor. But, I have a problem when I declare instance variable of array in class. It will be error if I make it like this:
public class Data {
private String [][] tabel;
public Data(){
tabel = {{"ID", "NAME"},
{"101", "Max"},
{"102", "Mark"},
{"103", "Downey"},
{"104", "Matthew"},
{"105", "Richard"}};
}
How I can solve this problem?
You need to write new Type[] in front of the array initializers like so:
tabel = new String[][]{
new String[]{"ID", "NAME"},
new String[]{"101", "Max"},
new String[]{"102", "Mark"},
new String[]{"103", "Downey"},
new String[]{"104", "Matthew"},
new String[]{"105", "Richard"}};
This question already has answers here:
Java ArrayList copy
(10 answers)
Closed 6 years ago.
I have an array list of:
ArrayList<ItemsBean> itemsList1 = new ArrayList<ItemsBean>();
I called the data from data base and added to this Arraylist
while (items.next()) {
ItemsBean bean = new ItemsBean();
bean.setInvNo(items.getString("Invoice_Number"));
bean.setItemnNameDisplay(items.getString("Prodname"));
bean.setParentobjectid(items.getString("ParentObjectID"));
bean.setQuantityDisplay(items.getInt("Quantity"));
bean.setProdnum(items.getInt("ProdNum"));
itemsList1.add(bean);
}
Now I have new array list:
ArrayList<ItemsBean> newListitems2 = new ArrayList<ItemsBean>();
now I want to pass same data to this new array list in same activity
You can either use addAll() or try like this arraylist2 = arraylist1 .
addAll() is like below
arraylist2.addAll(arraylist1);
Hope this is helpful :)
ArrayList newListitems2 = new ArrayList(itemsList1);
ArrayList has constructor, receiving Iterable<> as argument, so you cancreate new list with values from another list. Notice, values will remain the same, ArrayList only contains references to actual objects.
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]);
}
This question already has answers here:
Initialization of an ArrayList in one line
(34 answers)
Closed 8 years ago.
Sorry i really dont know how to do this, last resort but to ask.
I wanted to add values to the string list. But my code is not working
private List<String> sample = new ArrayList<String>(){"item1","item2","item3"};
Here it is:
List<String> sample = new ArrayList<String>(Arrays.asList("item1", "item2", "item3"));
Try,
ArrayList<String> list = new ArrayList<String>() {{
add("item1");
add("item2");
add("item3");
}}
OR
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
list.add("item3");