0

Why am i get the error "object[] cannot be converted to Integer[]" in line 28.please when should one use int and integer?

Method to reverse an array https://code.sololearn.com/cvEuBk99uvss/?ref=app

12th Aug 2019, 6:55 PM
Saitek
Saitek - avatar
2 odpowiedzi
+ 1
. toArray() without parameter returns Object but you want Integer, . with Integer[] array as parameter, stores result to this array with type Integer. Integer[] lastval = new Integer[len]; List<Integer> list = Arrays.asList(chara); ... lastval = list.toArray(lastval);
12th Aug 2019, 10:47 PM
zemiak
+ 1
This should work: //2nd methd public static Integer[] reversArr(Integer[] chara){ int len=chara.length ; List<Integer> list=Arrays.asList(chara); Integer[] lastval = new Integer[list.size()]; Collections.reverse(list); System .out.println(list); lastval=list.toArray(lastval); return lastval ; } * toArray returns an object array: Object[] (Object is the mother class of all classes) * if you just write List list then the list uses Object as data type -> use List<Integer> More about converting list to array: https://www.google.de/amp/s/www.geeksforgeeks.org/arraylist-array-conversion-java-toarray-methods/amp/ In your code you can just write int[] ... int is a primitve data type. Integer is a wrapper class. It converts the primitve data type into an object. https://www.google.de/amp/s/www.geeksforgeeks.org/wrapper-classes-java/amp/ I think you only need Integer if you want to use methods of this class. int[] is an integer array. Arrays are always objects.
12th Aug 2019, 10:55 PM
Denise Roßberg
Denise Roßberg - avatar