Say I want to create a List which contains Integer[] arrays. But
Integer[] foo = {1,2,3};
List<Integer[]> bar = Arrays.asList(foo);
The second line wouldn't compile, because Arrays.asList(foo) will return a List with three Integer elements(namely 1, 2, 3), not a List with a single Integer[] element.
Since the documentation states the parameter of asList method as varargs of type T, I don't understand why the compiler doesn't interpret the second line as single argument of type Integer[] given. How do I get a List of single Array element?
List<Integer[]> bar = new ArrayList<Integer[]>(Arrays.asList(foo));
This is what I actually wanted to do, but it also doesn't compile, for the same reason I believe.
A very similar question was just asked the other day, but I suppose this is different enough for it not to be a duplicate. You need to explicitly specify the generic type for it to compile, like so:
Integer[] foo = {1, 2, 3};
List<Integer[]> list = Arrays.<Integer[]>asList(foo);
Unless you actually need the ability to set the element to a different array, you can use:
List<Integer[]> list = Collections.singletonList(foo);
This is more efficient, because it is specialized to just holding one element: no array has to be created, contains is simply checking equality, size is always 1 etc.
No. Arrays.asList takes a projection of your current array and flattens the first dimension into a List. Since you only have one dimension, all of those elements get collected into the list.
You could do it if you had an Integer[][].
Note: asList accepts a vararg of T (so T...), which is effectively T[]. If you substitute that with Integer[][], you get Integer[], since one dimension of your Integer[][] will satisfy T...'s type requirement.
Varargs in java is just a syntactic sugar in Java. So, there is no difference between.
Arrays.asList(1,2,3) and Arrays.asList(new Integer[]{1,2,3}).
So what you can do like below to solve your problem.
Integer[][] foo = {{1,2,3}};
List<Integer[]> bar = Arrays.asList(foo);
Arrays.asList() generates list of contents of array passed as argument. If you need to create list of arrays, you need to pass to this method array of arrays (e.g. Integer[][]).
When you pass to method Integer[] as argument instead of Integer[][] , it makes a list of contents of that array (in that case these are Integers not arrays of Integers).
Wrapping your array in another array will achieve what you want:
Integer[] foo = {1,2,3};
List<Integer[]> bar = Arrays.asList(new Integer[][]{foo});
// OR
Integer[][] foo = {{1,2,3}};
List<Integer[]> bar = Arrays.asList(foo);
You could also just manually set the first element: (I'd probably say this is more readable, but opinions may vary)
Integer[] foo = {1,2,3};
List<Integer[]> bar = new ArrayList<>();
bar.set(0, foo);
Related
When I create a Java list and I want that the head is a list, should it print a [[-1,0],1,2,3,4] or its alright it just leave the sublist [-1, 0] as two separated elements like [-1, 0, 1, 2,3,4] and, how can I get the first structure as an aswer.
From your post I guess you are asking about how to create a list "construct" in Java which contains both list and non-list elements.
In Java all elements of your list/array need to have the same type. In the example you posted ([[-1,0],1,2,3,4]), the elements are not all the same type. What you have is this: [List<Integer>, Integer, Integer, Integer, Integer]. The first one is different, which isn't allowed in Java.
So, what you need to do is make it so that all elements of your list/array are the same type.
For your case, where there are only integers in your list, the easiest thing to do is to change your types to this: [List<Integer>, List<Integer>, List<Integer>, List<Integer>, List<Integer>] (with your specific values: [[-1,0],[1],[2],[3],[4]]). Instead of a list of List<Integer> and Integer, you now have a list containing only List<Integer> (some of your lists happen to contain only 1 element, but that is ok).
This is called a "two-dimensional list" and depending on whether you want to code it with arrays or java.util.Lists you can code it in one of the following ways:
// with arrays
int[] myTwoDarr = int[5][];
myTwoDarr[0] = new int[]{-1, 0};
myTwoDarr[1] = new int[]{1};
myTwoDarr[2] = new int[]{2};
myTwoDarr[3] = new int[]{3};
myTwoDarr[4] = new int[]{4};
// With Lists
List<List<Integer>> myList = new LinkedList<>();
List<Integer> nestedList1 = new LinkedList<>();
nestedList.add(-1);
nestedList.add(0);
myList.add(nestedList1);
List<Integer> nestedList2 = new LinkedList<>();
nestedList2.add(1);
myList.add(nestedList2);
...etc...
Now, if you absolutely must have different kinds of data in your list, you can create a list of Object instead.
In Java Object is a type from which all non-primitive types inherit. Thus, as long as something is not a primitive, it is an Object and can go in a list of Object. This will allow you to put any kind of data in the elements of your list. Using Object allows you to have a list of all the same type which looks like this: [Object, Object, Object, Object, Object]. But, now Java does not know the specific types you actually have stored in your elements so you will need to type cast when getting them.
Here's an example using a java.util.List:
List<Object> myList = new LinkedList<>();
List<Integer> nestedList = new LinkedList<>();
nestedList.add(-1);
nestedList.add(0);
myList.add(nestedList);
myList.add(1);
myList.add("a string");
// You need to cast when taking elements out of the list.
List<Integer> newNest = (List<Integer>) myList.get(0);
Integer myInt = (Integer) myList.get(1);
String myString = (String) myList.get(2);
Since all your base data types are the same (Integer/int) I recommend avoiding the Object list method and going with the 2D list. It's simpler and less error prone to not do all this casting.
I wrote following code and was surprised to see the output:
Integer a = 211;
int b = 211;
int[] array = {210,211,212};
System.out.println(Arrays.asList(array).contains(a));
System.out.println(Arrays.asList(array).contains(b));
Output:
false
false
I found this question and some other questions linked to it and learned that asList method doesn't Autobox stuffs. I checked the returned type in eclipse javadoc preview:
I couldn't quite understand this return type. int[] is an object and not a primitive so its fine. I'm sure that I'm not getting List<Integer> (something which I expected) but I'm not sure how to use the thing which is being returned. My questions are:
1. How exactly do I expect that list methods will work when I'm expecting an List of Integer and getting a List of int[] ?
2. In case of Strings the return type is List of String and not List of String[]. What sort of implementation differences are there?
3. What good is this method for primitives if things are so uncertain?
There are obviously 3 questions here so lets tackle them one by one:
How exactly do I expect that list methods will work when I'm expecting an List of Integer and getting a List of int[] ?
Well, List methods will work exactly as expected, a List<T> is a list of types T. Here T is an int[] so a List<int[]> will contains arrays as each element:
[{1, 2}, {3, 4}, {1, 6}]
So get(i) will return the ith element. In the case of Arrays.asList the List contains a single element, namely the int[] so:
int[] array = {210,211,212};
List<int[]> list = Arrays.asList(array);
Will be
[{210, 211, 212}]
And so
list.get(0)[0] == 210
In case of Strings the return type is List of String and not List of String[]. What sort of implementation differences are there?
String is an Object, not a primitive type. The difference follows from that.
What good is this method for primitives if things are so uncertain?
Things are not uncertain. This method results in defined and predictable behaviour. It's just not very useful for primitives. This is (yet another) side effect of combining Java's type system with generics.
Note with Java 8 the conversion of an int[] to a List<Integer> is very simple:
List<Integer> list = Arrays.stream(array).
boxed().
collect(toList());
You are not getting a Lit or a List (which can't be), you're getting a List of arrays of integer.
So your list does not contain 211, it contains an array that then contains 211.
The array is not "unrolled" into the list, it is added "as is" to a newly created list.
So :
System.out.println(Arrays.asList(array).contains(array)); // Will return true
System.out.println(Arrays.asList(a).contains(a)); // Will return true
It is because Arrays.asList() is a variadic generic function. Change this,
int[] array = {210,211,212};
to
Integer[] array = { 210, 211, 212 };
And the output will be true and true.
Arrays.asList takes objects as params. Since int[] is an object, you get the List.
If you want a list of integers you should do Arrays.asList(211,212,213).
int[] array = {210,211,212};
Arrays.asList(array)
is equal to
List<int[]> asList = Arrays.asList(array);
Change
int[] array = {210,211,212}; to Integer[] array = {210,211,212}; and it will work.
and its equals to List<Integer> asList = Arrays.asList(array);
I was wondering if it is possible to convert an Object into something else.
I have a Object which contains a series of numbers in a random order such as: 3, 4, 2, 5, 1 and wondering if I am able to turn it into an int[] or select certain elements from it, as in a number from the sequence?
EDIT:
so some of the code i have is:
//This contains all the different combinations of the numbers
ArrayList routePop4 = new ArrayList();
//This picks out the first one, just as a test
Object test = routePop4.get(0);
But the idea is that I want to loop through each element of test.
An Object cannot "contain a series of numbers". However many subclasses of Object, such as all of the Collections can "contain a series of numbers", and they come with a toArray() method to turn the contents of the collection into an array.
If you have a collection, but only have access to it as an Object, you need to cast it before you can work with it properly:
ArrayList<Integer> list = (ArrayList<Integer>)test;
Integer[] arr = list.toArray(new Integer[]{});
It's fairly rare in day-to-day Java to actually be working with variables cast as Object, if you are, it should be a red flag that you may be doing something wrong. You can use generics to allow objects that contain other objects to do so generically, like so:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1); // Can only add integers, list.add("a string") would fail at compile time
int n = list.get(0); // no need to cast, we know list only contains Integers
If you aren't using a Collection, you'll presumably need to roll your own, as Luke Taylor's answer suggests. That said, you'll get better answers if you can provide more information, the current text of your question doesn't make sense in a Java context.
After seeing your edit, I recommend taking advantage of generics.
When you declare an ArrayList you can indicate what kind of objects it's going to contain.
For example, if you know your ArrayList will contain Strings, you would do this:
List<String> myList = new ArrayList<String>();
If each element of your list is an array of Integers, you would do this:
List<Integer[]> listOfIntegerArrays = new ArrayList<Integer[]>();
Then you could get any element from your list and assign it to an Integer array like this:
Integer[] integerArray = listOfIntegerArrays.get(0);
Then you could iterate over every Integer in the list like this:
for (Integer loopInteger : integerArray) {
System.out.println("The value: " + loopInteger);
}
Some more reading on generics:
http://thegreyblog.blogspot.com/2011/03/java-generics-tutorial-part-i-basics.html
http://docs.oracle.com/javase/tutorial/java/generics/
You could do something like this:
int[] numbersFromObject = new int[yourObject.getAmountOfNumbers()];
// Initialize array with numbers from array
for(int i = 0; i < yourObject.getAmountOfNumbers(); i++) {
numbersFromObject[i] = yourObject.getNumber(i);
}
I'm not sure what methods your object contains, yet I'm sure you'll be able to adjust to the following mentioned above.
I hope this helps.
Consider this code:
int[] tcc = {1,2,3};
ArrayList<Integer> tc = Arrays.asList(tcc);
For the above, Java complains that it cannot convert from List<int[]> to ArrayList<Integer>.
What's wrong with this?
Why is it List<int[]> and not List<int>?
An ArrayList can hold only objects not primitives such as ints, and since int != Integer you can't do what you're trying to do with an array of primitives, simple as that. This will work for an array of Integer though.
This will work:
ArrayList tc = new ArrayList(Arrays.asList(1,2,3));
You could have it as:
List<int[]> tc = Arrays.asList(tcc);
Arrays.asList returns a List, not an ArrayList. And since Arrays.asList is a varargs function, it thinks tcc is one element of a bigger array.
If you want to have just a List of Integers, you'd have to rewrite it as SB mentioned in the comments for Hovercraft Of Eel's answer:
List<Integer> tc = Arrays.asList(1, 2, 3);
Alternatively, if you make tcc an Integer[], you can still use your array as an argument in the following snippet by explicitly asking for a List of Integer, providing a type parameter that agrees with the passed array:
Integer[] tcc = {1,2,3};
List<Integer> tc = Arrays.<Integer>asList(tcc);
I expected this code to display true:
int[] array = {1, 2};
System.out.println(Arrays.asList(array).contains(1));
The method Arrays.asList(T ...) is, when generics are erased and varargs are transformed, actually equal to a method of type Arrays.ofList(Object[]) (which is the, binary equivalent, JDK 1.4 version of the same Method).
An array of primitives is an Object (see also this question), but not an Object[], so the compiler thinks you are using the varargs version and generates an Object array around your int array. You could illustrate what's happening by adding an extra step:
int[] array = {1, 2};
List<int[]> listOfArrays = Arrays.asList(array);
System.out.println(listOfArrays.contains(1));
This compiles and is equivalent to your code. It also obviously returns false.
The compiler translates varargs calls into calls with a single array, so calling a varargs method that expects parameters T ... with parameters T t1, T t2, T t3 is equivalent to calling it with new T[]{t1, t2, t3} but the special case here is that varargs with primitives will be autoboxed before the array is created if the method needs an object array. So the compiler thinks the int array is passed in as a single Object and creates a single element array of type Object[], which it passes to asList().
So here's the above code once again, the way the compiler implements it internally:
int[] array = {1, 2};
// no generics because of type erasure
List listOfArrays = Arrays.asList(new Object[]{array});
System.out.println(listOfArrays.contains(1));
Here are some good and bad ways to call Arrays.asList() with int values:
// These versions use autoboxing (which is potentially evil),
// but they are simple and readable
// ints are boxed to Integers, then wrapped in an Object[]
List<Integer> good1 = Arrays.asList(1,2,3);
// here we create an Integer[] array, and fill it with boxed ints
List<Integer> good2 = Arrays.asList(new Integer[]{1,2,3});
// These versions don't use autoboxing,
// but they are very verbose and not at all readable:
// this is awful, don't use Integer constructors
List<Integer> ugly1 = Arrays.asList(
new Integer(1),new Integer(2),new Integer(3)
);
// this is slightly better (it uses the cached pool of Integers),
// but it's still much too verbose
List<Integer> ugly2 = Arrays.asList(
Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)
);
// And these versions produce compile errors:
// compile error, type is List<int[]>
List<Integer> bad1 = Arrays.asList(new int[]{1,2,3});
// compile error, type is List<Object>
List<Integer> bad2 = Arrays.asList(new Object[]{1,2,3});
Reference:
Java Tutorial > Classes and Objects > Passing Information to a Method or a Constructor > Varargs
Arrays.asList(T ...)
But to actually solve your problem in a simple way:
There are some library solutions in Apache Commons / Lang (see Bozho's answer) and in Google Guava:
Ints.contains(int[], int) checks whether an array of ints contains a given int
Ints.asList(int ...) creates a List of Integers from an int array
The Arrays.asList(array) will result in a singleton list of an int[].
It works as you expect if you change int[] to Integer[]. Don't know if that helps you though.
Arrays.asList(ArrayUtils.toObjectArray(array))
(ArrayUtils is from commons-lang)
But if you want to just call contains there is no need of that. Simply use Arrays.binarySearch(..) (sort the array first)
This
System.out.println(Arrays.asList(array).contains(array));
returns true.
It seems like your understanding of Arrays.asList(T... a) is wrong. You wouldn't be the first person to make an assumption as to how it works.
Try it with
System.out.println(Arrays.asList(1, 2).contains(1));
Autoboxing just doesn't work the way you want it to in this case. The following code may be a bit verbose, but does the job of converting an int array to a list:
List<Integer> list = new ArrayList<Integer>(array.length);
for (int value : array) {
list.add(value);
}
The following code displays true:
Integer[] array = {1, 2};
System.out.println(Arrays.asList(array).contains(1));
(Your version fails, since Int's not beeing objects, but Int[] is an object. Therefor you will call asList(T... a) with one element beeing a Collection, since it is not possible to have an Collection a.)
When you call
Arrays.asList(array)
on your array of primitives, you get a List instance containing one object: an array of int values! You have to first convert the array of primitives into an array of objects, as #Bozho suggests in his answer.
If you only want to check whether the array contains certain element just iterate over array and search for element. This will take o(n/2). All other solutions are less effective. Any method that copies array to list must iterate over array and therefore this operation only requires n atomic assignments.
I dont think there is a method call you could use. Try it like this
List<Integer> list = new ArrayList<Integer>();
for (int index = 0; index < array.length; index++)
{
list.add(array[index]);
}