Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is done thusly</p> <pre><code>public class myClass{ int arraysize = 2; float[] myArray; // Declare array public myClass(){ myArray = new float[arraySize]; // initialize array } public float[] accessArray(){ return myArray; } } </code></pre> <p>The array declaration must not be done inside the class methods. Variable declaration done inside a method limits it's scope of a variable to the method. (i.e you can't use it anywhere else).</p> <p>The array is then instantiated in a constructor. A constructor is a special function that is run when a class is instantiated. Constructor are used to instantiated a class's variables Constructors have the same name as their class and must not specify a return type (so no public int or public void just public)</p> <p>Next you need to change the return type of the accessArray method. A return type of void states that the method isn't going to return anything. Change it to float[] Then your accessArray method need only return the array variable.</p> <p>EDIT: The "return myArray;" line of code gives a reference to the array to what ever called the function (Not a copy of the array, the actual array, a quick of Java is that it always does this except when returning primitive data types where it returns a copy)</p> <p>If you want accessArray() to set floats in the array instead of returning the array it should be implmented like this.</p> <pre><code>public void accessArray(int index, float value){ myArray[index] = value; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload