-1

I am new to programming and am just trying to work a bit with arrays. I am in the process of making the methods I want to use on the array, but when I try to pass the actual array through these methods I get the error in the title. What am I doing wrong?

public class ArrayPractice extends Arrays
{
    double[] values = new double[11];

    public static void main(String[] args)
    {
        //when methods are created run them each once for the array we created and     then print out the result.
        fillArray(values);
        firstLastSwap(values);
        System.out.println(values);

    }


    public void fillArray(double[] array)
    {
        for(int i=0; i<array.length; i++)
        {
            values[i] = Math.random();
        }
    }

    public void firstLastSwap(double[] array)
    {
        double tempOne = array[0];
        double tempTwo = array[array.length-1];
        for(int i=0; i<array.length; i++)
        {
            if(i == array[0])
            {
                array[i] = tempTwo;
            }
            else if(i == array[array.length-1])
            {
                array[i] = tempOne;
            }
        }
    }

    public void shiftElementsLeft(double[] array)
    {

    }

    public void evenElementsToZero(double[] array)
    {

    }

    public void replaceWithBigNeighbor(double[] array)
    {

    }



}
Xerunix
  • 431
  • 1
  • 6
  • 21

2 Answers2

0

Put the keyword "static" in front of your global variable:

static double[] values = new double[11];

Main() is a static method and it can only use static fields or variables defined within it.

Denis
  • 11,796
  • 16
  • 88
  • 150
0

Option 1. Make your methods static that you are accessing in main() method.

option 2: Create an instance of the class in the main() method and access non-static methods using the instance

ArrayPractice AP = new ArrayPractice();
AP. fillArray(values);
Pankaj Gadge
  • 2,748
  • 3
  • 18
  • 25