how to convert a collection into an int array
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   kannan_Chandran
Posted On:   Monday, November 7, 2005 07:34 AM

class colA_list{ public static void main(String args[]){ ArrayList list=new ArrayList() ; list.add(new Integer(1)); list.add(new Integer(2)); list.add(new Integer(3)); list.add(new Integer(4)); list.add(new Integer(5)); System.out.println("Hai Array"+list); ListIterator itr = list.listIterator(); int [] ii=new int[list.size()]; if(itr.hasNext()){ Integer iP=(Integer)itr.next(); ii=(int [])iP.intValue(); System.out.println("Question "+ii); } } } i got error in this code while i try t o type cast int to an int array    More>>

class colA_list{
public static void main(String args[]){
ArrayList list=new ArrayList() ;
list.add(new Integer(1));
list.add(new Integer(2));
list.add(new Integer(3));
list.add(new Integer(4));
list.add(new Integer(5));
System.out.println("Hai Array"+list);
ListIterator itr = list.listIterator();
int [] ii=new int[list.size()];
if(itr.hasNext()){
Integer iP=(Integer)itr.next();
ii=(int [])iP.intValue();
System.out.println("Question "+ii);
}
}
}
i got error in this code while i try t o type cast int to an int array

   <<Less

Re: how to convert a collection into an int array

Posted By:   rahul123  
Posted On:   Wednesday, December 18, 2013 10:58 PM

class colA_list{
public static void main(String args[]){
ArrayList list=new ArrayList() ;
list.add(new Integer(1));
list.add(new Integer(2));
list.add(new Integer(3));
list.add(new Integer(4));
list.add(new Integer(5));
System.out.println("Hai Array"+list);
ListIterator itr = list.listIterator();
int [] ii=new int[list.size()];
int i=0;
if(itr.hasNext()){
Integer iP=(Integer)itr.next();

ii[i]=iP.intValue();
i++;
System.out.println("Question "+ii);
}
}

Re: how to convert a collection into an int array

Posted By:   Jani_Kaarela  
Posted On:   Tuesday, November 8, 2005 03:25 AM


Man, you're awesome! But seriously, please, pleasepleaseplease, read the ****ing manual before posting. By learning to use the API docs, you're doing yourself a favour and taking the first step towards becoming an actual coder.



Once you've gotten over the anger I've caused by stating the obvious, check out this method in the API...

Re: how to convert a collection into an int array

Posted By:   Almagest_FUTT  
Posted On:   Monday, November 7, 2005 07:59 AM

In your unformatted code, you're not trying to convert a Collection to an int[], instead you're casting an Integer object's int value, that is a primitive, to an int[] object.

Apart from being nonsense, this gives you a runtime error. Working as designed.
About | Sitemap | Contact