简体   繁体   中英

Trying to assign a 1 dimensional array from an arraylist to another variable

So I have an arraylist whose elements are 1 dimensional arrays, and I am trying to access that arraylist, and save it's first element on another variable, using the following piece of code, but the compiler gives me an "incopatible types, Object cannot be converted to int" error

int[] k=new int[3];
k[0]=0;
k[1]=0;
k[2]=0;
ArrayList qu=new ArrayList();
qu.add(0,k);
int[] j=new int[3];
j= qu.get(0);

If you are not using generics, do an explicit cast

j= (int[]) qu.get(0);

or you can use generics. Notice that arrays are objects in java and hence can be used as type parameters.

ArrayList<int[]> qu=new ArrayList<int[]>();
qu.add(0,k);
int[] j=new int[3];
j= qu.get(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM