1

program counts the frequency of max number but the conversion of array into list gives list=[I@7852e922 instead of what it should actually return.

static int birthdayCakeCandles(int[] ar) {
        Arrays.sort(ar);
        for(int i=0;i<ar.length;i++)
        {
            System.out.print(ar[i]);  
        }

        int max=ar[ar.length-1];
        System.out.println("\n"+max);
        List list = Arrays.asList(ar);
        for(int i=0;i<list.size();i++)
        {
            System.out.println(list.get(i));  
        }
        int sol=Collections.frequency(list,max);
        return sol;

    }
    public static void main(String[] args) {
        int arr[]={1,2,3,4,4,2};

    int ans=birthdayCakeCandles(arr);
    System.out.print(ans);
    }

the output that this gives is:

122344
4
[I@7852e922
0

which is not correct as the array to list conversion is not taking place in the right way.

  • Does this answer your question? [Print ArrayList](https://stackoverflow.com/questions/9265719/print-arraylist) – jhamon Jan 13 '20 at 10:14
  • "is not working", can you be more specific about that? – Stultuske Jan 13 '20 at 10:16
  • 4
    Change `int arr[]` to `Integer arr[]` if you want Arrays.asList to produce a List of Integer (and not a List of int[]) – Eran Jan 13 '20 at 10:16
  • @gauravgupta if you got the answer please accept the answer: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Sumit Singh Jan 13 '20 at 11:58

3 Answers3

2

From Arrays.asList(int[]) not working

When you pass an array of primitives (int[] in your case) to Arrays.asList, it creates a List<int[]> with a single element - the array itself.

The signature of asList is List<T> asList(T...). A primitive can't replace a generic type parameter. Therefore, when you pass to this method an int[], the entire int[] array replaces T and you get a List<int[]>. On the other hand, when you pass an Integer[] to that method, Integer replaces T and you get a List<Integer>.

Thats why in your case it Collections.frequency(list,max) was returning 0.

Change your function like below:

static int birthdayCakeCandles(int[] ar) {
        Arrays.sort(ar);
        for(int i=0;i<ar.length;i++)
        {
            System.out.print(ar[i]);  
        }

        int max=ar[ar.length-1];
        // convert your int ar to Integer List
        List<Integer> list = Arrays.stream(ar).boxed()// Stream<Integer>
                              .collect(Collectors.toList());
        System.out.println("\n"+max);
        int sol=Collections.frequency(list,max);
        return sol;

    }
Community
  • 1
  • 1
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
1

Arrays.asList will not work as you expect.

From Java 8 onwards:

List<Integer> list = Arrays.stream(ar).boxed().collect(Collectors.toList());
Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
Anton Dikarev
  • 335
  • 3
  • 11
0

You can’t use the popular Arrays.asList to convert it directly, because boxing issue.

For this to work, you can make below changes in the code.

public static void main(String[] args) {
        Integer arr[]={1,2,3,4,4,2};

        int ans=birthdayCakeCandles(arr);
        System.out.print(ans);
}
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89