2

I don't understand, and can't find on google how do I call my method, whose parameter is an array?

private static void printArray(double a[],int p){
        int count[]=new int[p];
        for(int i=0;i<a.length;i++){
            for(int j=0;j<p;j++){
                if((a[i]>=100/p*j) && (a[i]<100/p*(j+1))){
                    count[j]++;

for example, how do I call this method in my main method:

I tried printArray({1,2,3,4,5},5); and it's not working or printArray([10],5); but still doesn't work?

Atri
  • 5,511
  • 5
  • 30
  • 40
Buzz Black
  • 29
  • 1

4 Answers4

4

You would first need to create an array. Like you did here:

int count[]=new int[p];

In your example something like

double myArray[] = {1,2,3,4,5};

Then you would pass the variable name to your method like:

printArray(myArray, someInt);

Hope this helps.

Kyle Gowen
  • 467
  • 2
  • 8
1

The issue with your calls is:

  • printArray({1,2,3,4,5},5); - {1,2,3,4,5} as a parameter is not recognized. You need to create an array which is of type double.
  • printArray([10],5); - [10] is also not recognized by the compiler and is not the correct way to pass double[].

This is how to initialize an array inline in java: new double[]{1,2,3,4,5}

So, this is how you call it:
printArray(new double[]{1,2,3,4,5},5);

Atri
  • 5,511
  • 5
  • 30
  • 40
0

you can call your method like this, if the method is in the same class with method main.

 double[] arr = {1,2,3,4,5};
 printArray(arr,5);
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
0

[10] is not a variable. When you pass an array you do not need brackets. Just the variable name. For example:

double array1[] = {10,12,13};
printArray(array1);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
Chris
  • 62
  • 11