How to call a method when its parameter is an array?

2

I'm new to java and I'm having a hard time calling a method, well, I have this:

class main {
    public static void main(String[] args) {
        vetorFloat(vetor[x]);
    }
    private float vetorFloat(float[] vetor) {
        //algum código
        return vetor[x];
    }
}

The vetorFloat method is in the same class as the main main method.

How could I call this method?

    
asked by anonymous 28.03.2014 / 01:47

2 answers

2

So:

float[] floatArray = new float[]{1.0f, 2.2f, 3.4f};
vetorFloat(floatArray);

So the method and class in static context would look like this:

class main {
    public static void main(String[] args) {
        float[] floatArray = new float[]{1.0f, 2.2f, 3.4f};
        vetorFloat(floatArray );
    }
    private static float vetorFloat(float[] vetor) {
        //algum código
        return vetor[x];
    }
}

Except that X in return vetor[x]; will give compilation problem. It needs to be solved. It may look like this:

class Main {
    public static void main(String[] args) {
        float[] floatArray = new float[]{1.0f, 2.2f, 3.4f};
        vetorFloat(floatArray );
    }
    private static float vetorFloat(float[] vetor) {
        int x = 0;
        return vetor[x];
    }
}

Now your method will return the first position of the last array.

The way you did, you're passing an element of the array, not the whole array. In my example, I declare a new array and I already fill it in. So the variable floatArray represents the entire array and if I do floatArray[0] , I'm only accessing the first element of the array, which in this case is 1.0.

That "F" there next in the number, it is useful for Java to know that it is a type float . When you fill in an arbitrated number in the code, java does not know if it treats as int , float , long ... then you specify after declaring the number. Examples:

  • long 0L
  • float 0.0f
  • double 0.0d

See official documentation on primitive types . It can help a lot.

    
28.03.2014 / 01:51
2

In your main() method you make three errors:

  • Uses undeclared variables: vetor and x
  • Pass an argument of type float to a method that has an array of float s as parameter ( float[] is a vector of float s)
  • You call a non-static method ( vetorFloat() ) from a static method ( main() ).
  • You also do other things that are not errors, but you should know:

  • According to the convention used in Java, classes must begin with a capital letter.
  • You're not using the return method vetorFloat() for anything (this you probably already noticed, but I could not stop quoting).
  • Repairing

    This is just my suggestion, it fixes the errors and the warning I mentioned above:

    class Main {
        public static void main(String[] args) {
            float[] vetor = {1, 2, 5};
            System.out.println(vetorFloat(vetor));
        }
        private static float vetorFloat(float[] vetor) {
             return vetor[vetor.length-1]; //retorna o último elemento do vetor
        }
    }
    

    Note: Do not forget to rename your file to Main.java

        
    28.03.2014 / 12:27