The error is, that it is showing a red line in Collections.addAll(in,n).
How do I solve this?
ArrayList<Integer> in = new ArrayList<>(); //declared array in integer type
int[] n = getResources().getIntArray(R.array.number12);
Collections.addAll(in,n); //this is not working showing error,it is not accepting
integers.xml
<resources>
<integer-array name="number12">
<item>735895698</item>`
<item>814895046</item>``
</integer-array>
</resources>
This doesn't work, because an int[]-array is not an Integer[]-array, which Collections.addAll() expectes.
here are two ways to create a list from an array. This uses a for-each loop which iterates over the array and then adds every int in the array to the list:
int[] n = getResources().getIntArray(R.array.number12);
List<Integer> list = new ArrayList<>(n.length);
for(int i : n){
list.add(i);
}
The other way, is to use the in Java8 introduced Arrays.stream():
int[] n = getResources().getIntArray(R.array.number12);
List<Integer> list = Arrays.stream(n)
.boxed()
.collect(Collectors.toList());
And if you explicitly want to get an ArrayList you can use the following:
int[] n = getResources().getIntArray(R.array.number12);
List<Integer> list = Arrays.stream(n)
.boxed()
.collect(Collectors.toCollection(ArrayList::new));
Quick fix:
ArrayList<Integer> in= new ArrayList<>();//declared array in integer type
Integer[] n=getResources().getIntArray(R.array.number12);
Collections.addAll(in,n);//this is not working showing error,it is not
As explained here, the type needs to be the same.
The Collection expects an array of Integer, not an array of int
Related
I have the below piece of code for swapping.
public static <E> void swap(List<E> list, int i, int j){
E temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
Now when I use List backed by Integer array like below
Integer[] ar = new Integer[]{1,2};
swap(Arrays.asList(ar),1,0);
It works fine and gives output as [2,1]
But I use List backed by int array like below
int[] ar = new int[]{1,2};
swap(Arrays.asList(ar),1,0);
It hrows ArrayIndexOutOfBounds exception. I don't understand why this is happening. List should treat int element as object only. Little help please.
Here is what is happening. Your current code is actually creating a List<Object>, which happens to contain just a single int[], not a list of actual integers:
int[] ar = new int[]{1,2};
List<Object> list = Arrays.asList(ar);
The reason for the ArrayIndexOutOfBounds exception is that the list you pass in to the swap method has only one entry, at index zero.
In any case, it is not possible to use Arrays.asList to directly convert an array of primitives to a list of some boxed type. The first version of your code is correct, and is what you should be using:
Integer[] ar = new Integer[] {1, 2};
swap(Arrays.asList(ar), 1, 0);
So, this is part of a method which checks for available rooms within a date range and is meant to return the int [] array of room numbers which are available.
ArrayList roomNums = new ArrayList();
roomNums.toArray();
for (Room room: rooms){
int roomNumber = room.getRoomNumber();
if(room.getRoomType() == roomType && isAvailable(roomNumber, checkin, checkout)){ // This works fine, ignore it
roomNums.add(roomNumber);
}
}
return roomNums.toArray(); // Error here, return meant to be int [] type but it's java.lang.Obeject []
The error occurs at the end at roomNums.toArray()
I saw someone else do this one liner and it worked for them, why is it not for me?
Every element in roomNums is an integer. (I think)
What's the quickest and easiest way to print an integer array containing the available rooms? Do I need to make a loop or can I do something with this .toArray() one liner or something similar to it?
Thanks
Don't use raw types. Replace ArrayList roomNums = new ArrayList(); with
ArrayList<Integer> roomNums = new ArrayList<>();
Then return it as Integer[]
roomNums.toArray(Integer[]::new);
If you need primitive array, then it can be done with stream:
return roomNums.stream().mapToInt(Integer::valueOf).toArray();
See also How to convert an ArrayList containing Integers to primitive int array?
Unfortunately, you will need to use Integer[] if you want to use toArray
List<Integer> roomNums = new ArrayList<>();
// code here
Integer[] arr = new Integer[roomNums.size()];
return roomNums.toArray (arr);
However you can still do it manually like
List<Integer> roomNums = new ArrayList<> ();
// code here
int[] arr = new int[roomNums.size()];
for (int i = 0; i < arr.length; i++)
arr[i] = roomNums.get (i);
return arr;
As mentioned above use the ArrayList to hold your values:
ArrayList<Integer> roomNums = new ArrayList<>();
then you can use the object super class to convert to array
Object[] numbersArray = roomNums.toArray();
then just continue running the for loop and your program
I'm trying to create a two dimensional ArrayList which will store ints, strings & booleans.
I've got as far as entering the first int, but I get a red squiggle and the error "int cannot be converted to ArrayList".
ArrayList[][] qarray= new ArrayList [10][5];
qarray[0][0]= 1;
BTW, googling the phrase "int cannot be converted to ArrayList" is giving me exactly six results.
The error is correct.
Your array type is ArrayList. You can insert only ArrayLists in that array.
If you want to store int's, your declaration should be.
int[][] qarray= new int [10][5];
And also, as someone commented, you cannot store strings and booleans in this array anymore.
ArrayList[][] qarray= new ArrayList [10][5];
Basically your code creates 2 dimensional array list objects (50 array list objects).
qarray[0][0]= 1;
And you are trying to assign integer, where you need to create a ArrayList object. It expects something like
qarray[0][0]= new ArrayList();
However this would not meet your objective. The following piece of code could meet your objectives:
ArrayList[] qarray = new ArrayList[10];
qarray[0]= new ArrayList();
qarray[0].add(1);
qarray[1]= new ArrayList();
qarray[1].add(true);
qarray[2]= new ArrayList();
qarray[2].add("hello");
Try like this:
List<Integer> qarray = new ArrayList<>();
qarray.add(1);
First I believe that you need array and not ArrayList. 2d array can be created as following.
int[][] arr = new int [10][10];
Your next problem is that you tried to assign int constant 1 to variable of other type. The following example shows how to assign int to element of array
arr [0][0] = 1;
According to javadoc, you can not create arrays of ArrayList. Use 2D array instead.
If you need 2D ArrayList anyway, you should have tried this way:
ArrayList<ArrayList<Integer>> listOfLists = new ArrayList<>();
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(5);
listOfLists.add(list1);
listOfLists.add(list2);
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
you wan to add int,String and boolean then you can use
ArrayList<ArrayList<Object>> listOfLists = new ArrayList<ArrayList<Object>>();
it will help you
ArrayList<Integer>[][] list = new ArrayList[10][10];
list[0][0] = new ArrayList<>();
list[0][0].add(new Integer(10);
try like this.
I need to use an ArrayList, but I am not sure how to do some of these things that would be possible with a normal array.
1) This:
int[][] example1 = new int[10][20];
(An array with two arguments (10, 20)) is possible with normal arrays, but how to do it with an ArrayList.)
2) How to increase the value of an int on the list by 1, like this:
example2[3][4] ++;
ArrayList is dynamically growable list backed by array.
List<List<Integer>> list = new ArrayList<List<>>(10);
you can get an element of list by List#get.
List<Integer> innerList = list.get(3);
Integer integer = innerList.get(4);
Update value by List#set -
list.get(3).set(4,list.get(3).get(4)++);
NOTE : Integer class is immutable.
To mimic a multidimensional array using collections you would use:
List<List<Integer>> list = new ArrayList<>(); //Java 7
List<List<Integer>> list = new ArrayList<List<Integer>>(); //Pre Java 7
So lets say we create a List<List<Integer>> where the outer List contains 10 List<Integer> and the inner list contains 10 Integers. To set the fifth element on the fourth list:
public static void main(String[] args) {
List<List<Integer>> outer = new ArrayList<List<Integer>>();
for(int x = 0; x < 10; x++){
List<Integer> inner = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
outer.add(inner);
}
//Remember that Integer is immutable
outer.get(3).set(4, new Integer(outer.get(3).get(4)) + 1);
}
int[][] example1 = new int[10][20]; you can do it in arraylist by using this syntax :
ArrayList<ArrayList<Integer>> ex = new ArrayList<ArrayList<Integer>>();
example2[3][4] ++; This can be same in arraylist as by using this :
int val = (a.get(0).get(0)) + 1;
The equivalent of your declaration with an ArrayList is:
List<List<Integer>> example1 = new ArrayList<>();
You have to use Integer because Java Collections do not support primitive types. Check out this page of the Oracle docs for more information on Autoboxing and Unboxing.
Since an ArrayListcan grow dynamically, you don't need to give a size. If you want it to have an initial size, you can pass that as an argument to the constructor.
You can get elements from an ArrayList (or any Class implementing the List interface) by using the get() method with the index of the element as an argument.
Using example.get() on example1 will give you an object of the type List. You can then use get() again to get an Integer.
I have values that I'd like to add into an ArrayList to keep track of what numbers have shown up.
The values are integers so I created an ArrayList;
ArrayList<Integer[]> list = new ArrayList<>();
int x = 5
list.add(x);
But I'm unable to add anything to the ArrayList using this method.
It works if I use Strings for the array list. Would I have to make it a String array and then somehow convert the array to integers?
EDIT: I have another question. I'd like the list to only hold 3 values. How would I do so?
List of Integer.
List<Integer> list = new ArrayList<>();
int x = 5;
list.add(x);
You are trying to add an integer into an ArrayList that takes an array of integers Integer[]. It should be
ArrayList<Integer> list = new ArrayList<>();
or better
List<Integer> list = new ArrayList<>();
you are not creating an arraylist for integers, but you are trying to create an arraylist for arrays of integers.
so if you want your code to work just put.
List<Integer> list = new ArrayList<>();
int x = 5;
list.add(x);
you should not use Integer[] array inside the list as arraylist itself is a kind of array. Just leave the [] and it should work
Actually what u did is also not wrong your declaration is right . With your declaration JVM will create a ArrayList of integer arrays i.e each entry in arraylist correspond to an integer array hence your add function should pass a integer array as a parameter.
For Ex:
list.add(new Integer[3]);
In this way first entry of ArrayList is an integer array which can hold at max 3 values.
The [] makes no sense in the moment of making an ArrayList of Integers because I imagine you just want to add Integer values.
Just use
List<Integer> list = new ArrayList<>();
to create the ArrayList and it will work.
Here there are two different concepts that are merged togather in your question.
First : Add Integer array into List. Code is as follows.
List<Integer[]> list = new ArrayList<>();
Integer[] intArray1 = new Integer[] {2, 4};
Integer[] intArray2 = new Integer[] {2, 5};
Integer[] intArray3 = new Integer[] {3, 3};
Collections.addAll(list, intArray1, intArray2, intArray3);
Second : Add integer value in list.
List<Integer> list = new ArrayList<>();
int x = 5
list.add(x);
How about creating an ArrayList of a set amount of Integers?
The below method returns an ArrayList of a set amount of Integers.
public static ArrayList<Integer> createRandomList(int sizeParameter)
{
// An ArrayList that method returns
ArrayList<Integer> setIntegerList = new ArrayList<Integer>(sizeParameter);
// Random Object helper
Random randomHelper = new Random();
for (int x = 0; x < sizeParameter; x++)
{
setIntegerList.add(randomHelper.nextInt());
} // End of the for loop
return setIntegerList;
}