how to convert Vector<Integer> to an int[]? [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert List<Integer> to int[] in Java?
Is there some method to convert a Vector< Integer> to an int[]?
Thanks

toArray() will do the conversion for you. Check the link for the javadoc of all the methods Vector has. This will be the boxed Integer, not int, but you can work from there.

Integer[] sl = (Integer[]) myVector.toArray(new Integer[0]);

Vector uses objects and not primary types. so you can only convert to an Object[], to convert to a primary type array you'd have to use an additional step.
with no further comments on what's the point of your code, I would say that Integer[] would acomplish the same thing

You can use a loop to copy a vector into an int[]
Vector<Integer> vector = ....
int count = 0, ints[] = new int[vector.size()];
for(int i: vector) ints[count++] = i;

Related

Trying to covert int array to List using Arrays.asList gives wrong value [duplicate]

This question already has answers here:
Converting array to list in Java
(24 answers)
Closed 9 months ago.
So I am trying to convert an (primitive) int array to a List,
for(int i=0;i<limit;i++) {
arr[i]=sc.nextInt();
}
List list=Arrays.asList(arr);
System.out.print(list);
Doing this so prints values like this for example, [[I#59a6e353] . There is an option to add <Integer> before variable list but since the array is primitive int, it could not.
Is there any solution for this? Adding <int[]> before variable list isn't an option as I need to call a bounded wildcard method.
Arrays.asList(arr) does not creates a List<Integer> but a List<int[]> with a single element (your arr).
You have to declare your variable as Integer[] arr if you have to use 'Arrays.asList'.
The signature of method Arrays.asList is the following one:
public static <T> List<T> asList(T... a);
You can see that it needs an array of generics T in input.
Therefore you can pass an array of Integer and it works. Example:
List<Integer> list= Arrays.asList(new Integer[] {1,2,3,4});
System.out.print(list);
Output:
[1, 2, 3, 4]
If you pass int[] you can have just a List<int[]> because int is a primitive type and it cannot be used as generic. Example:
List<int[]> list= Arrays.asList(new int[] {1,2,3,4});
System.out.print(list);
Output:
[[I#6b1274d2]
I suggest you to read the answer in this question.

Why do Java generics work for Array of primitives but not primitive? [duplicate]

This question already has answers here:
Why don't Java Generics support primitive types?
(5 answers)
Why java does not autobox int[] to Integer[]
(5 answers)
Closed 1 year ago.
ArrayList<int> list1 = new ArrayList<int>(); // Error
ArrayList<int[]> list2 = new ArrayList<int[]>(); // Works fine
Generics do not work for primitive types but work fine with Array of primitives. Why?
Follow up:
The only reason I can think of it is because the Array is an object so generics work with them. But if that is so then why couldn't a primitive int array be autoboxed to corresponding Integer wrapper class automatically?
Integer arr = new int[5]; // Error
Integer[] arr = new int[5]; // Error
Integer a = 5; // Works fine (autoboxing)
If someone could shed some light on this would be highly appreciated or if I am missing something of how it's represented internally. Thanks.

Java autoboxing and unboxing issue [duplicate]

This question already has answers here:
How to convert int[] into List<Integer> in Java?
(21 answers)
Closed 5 years ago.
Java compiler takes care of casting primitive data types and their wrapper classes..But my doubt is that although java compiler performs type casting all by itself, why is it that it prints an error when I try to convert an Array to ArrayList with int array as parameter..Like:
int[] val = {1,2,3,4,5};
ArrayList<Integer> newval = new ArrayList<Integer>(Arrays.asList(val));
Error: no suitable constructor found for ArrayList(List<int[]>)
Why is the compiler not casting int to Integer?
You can use a IntStream to help you with the "boxing" of primitives
int[] a = {1,2,3,4};
List<Integer> list = IntStream.of(a)
.boxed()
.collect(Collectors.toList());
This will iterate the array, boxed the int into an Integer and then you just have to collect the Stream into a List with the Collectors.
You can't create a ArrayList<primitive types> Source: why you can't create a Arraylist of primitive types. Instead, use an adaptor class:
class Adapter{
private int[] value;
adapter(int[] value){
this.value = value;
}
public int[] getValue(){
return value;
}
}
And then add it to the ArrayList<Adapter> AL = new ArrayList<>();
There is no autoboxing here; perhaps you meant to do:
Integer[] val = {1,2,3,4,5};
int[] val = {1,2,3,4,5};
For Primitive arrays:
List<int[]> vv = Arrays.asList(val);
Will get List of arrays because autoboxing wont work when we try to convert the primitive array into list
For Object array type:
Integer[] val = {1,2,3,4,5};
List<Integer> vv = Arrays.asList(val);
Compiler will use autoboxing
Arrays.asList accepts an array of objects - not primitives (in many cases the compiler is smart enough to interchange through something called autoboxing - in this case not). Using a simple loop you can add the items of the array to the List.

Java Integer[] to int[] [duplicate]

This question already has answers here:
How can I convert List<Integer> to int[] in Java? [duplicate]
(16 answers)
Closed 8 years ago.
I have a List which I need to convert to an int array (int[])
Currently I am doing this:
List<Integer> filters = new ArrayList<Integer>();
// add some elements to filters
int[] filteres = new int[filters.size()];
for(Integer i=0 ; i<filters.size(); i++)
filteres[i] = filters.toArray(
new Integer[filters.size()])[i].intValue();
I think this looks like a messy workaround and that should be somehow else to do this.
So is there a better way to make such conversion?
This can be simplified using the List.get() from the List interface:
int[] filteres = new int[];
for(int i=0 ; i<filters.size(); i++)
//auto unboxing takes care of converting Integer to int for you, if it's not null.
filteres[i] = filters.get(i);
See How to convert List<Integer> to int[] in Java? (which is a duplicate by all means; see this answer in particular).
However, for sake of discussion, consider this similar alternative and notes.
List<Integer> filters = getFilters();
// Arrays must be created with a size: the original code won't compile.
int[] ints = new int[filters.size()];
// Use an Enhanced For Loop if an index lookup into the source
// is not required; i is merely a result of needing to index the target.
int i = 0;
for(Integer filter : filters) {
// Just use auto unboxing Integer->int; no need for intValue()
ints[i++] = filter;
}
The original code is terrible because filters.toArray(new Integer[filters.size()])[i] creates a new array from the list each loop before the index operation. This makes the complexity O(n^2)+ just for the copy! While this can be fixed by replacing the offending expression with filters.get(i), an indexing operation can be skipped entirely in this case.
Using an enumerable approach similar to shown above (i.e. with an enhanced for loop) has the advantage that it will continue to work efficiently over source collections which are not fast-indexable such as a LinkedList. (Even using the filters.get(i) replacement, a LinkedList source collection would result in a O(n^2) complexity for the transformation - yikes!)
Instead of this:
filteres[i] = filters.toArray(
new Integer[filters.size()])[i].intValue();
You can just retreive the element like this:
filteres[i] = filters.get(i);
int[] filteres = new int[filters.size()];
for(int i =0;i<filters.size();i++){
filteres[i]=filters.get(i);
}

How to convert an array list into a normal array and vice versa normal array into array list? [duplicate]

This question already has answers here:
Converting ArrayList to Array in java
(12 answers)
Closed 9 years ago.
I need to convert an array list of object type into a normal arrays.
ArrayList<object> list = new ArrayList<Object>();
list.add(4);
list.add(56);
list.add("two");
What is the easiest way to do this?
how can we change an existing array to an arraylist?
String st[]=new String[];
Integer in[]=new Integer[];
how can i convert this array into an array list of Object type so i can have both this arrays in one arraylist?
Suppose arrlist is an ArrayList. To convert it into array,try the follwing code.
Integer list[] = new Integer[arrlist.size()]; //arrlist is an ArrayList
list = arrlist.toArray(list2);
FOr more detailed example try this tutorial :
toArray method usage
Try,
Integer[] arr= list.toArray(new Integer[list.size()]);
You can use toArray()
ArrayList<Integer> list=new ArrayList<Integer>();
list.add(4);
list.add(56);
Integer[] arr=list.toArray(new Integer[list.size()]);
Try this:
import org.apache.commons.lang.ArrayUtils;
int[] intArray = ArrayUtils.toPrimitive(list.toArray(new Integer[0]))
This approach will make two complete copies of the sequence: one Integer[] created by toArray, and one int[] created inside toPrimitive
arraylist can be converted into array using toArray() mehod in java.
Check
http://coderspoint.com/index.php?topic=7.msg10#msg10

Categories