0

I am trying to store X axis position of mouse and Y axis position of mouse separately inside a two dimentional array and use it later but getting errors. I am new to arrays and i am trying to initialize a two dimentional array using for loops and instance variables/global variables. The error which i am getting says that "array constants can only be used in initializers". I went through the documentetions and changed the code but not got a solution.

Questions and documentations which i read to get a solution

My code:

package com.selenium;

import java.awt.*;

public class MouseAxis {
  int[][] mouseLocation ;
  int AxisX;
  int AxisY;

public void getAxisXAndY(int x, int y){
    AxisX=x;
    AxisY=y;
} // getAxisXAndY

public void mouseAxisPosition() throws AWTException{
    int x , y;
    PointerInfo pointer = MouseInfo.getPointerInfo();
    Point point = pointer.getLocation();
    x=(int)point.getX();
    y=(int)point.getY();
    getAxisXAndY(x,y);
} // mouseAxisPosition

public void storeMouseLocation(){
 for(int i=0; i<=20; i++){
      for(int j=i; j<=1; j++){
        int[][] temp = new int[j][i];
        temp[j][i]={AxisX,AxisY} ;
        mouseLocation[j][i]=temp[j][i];
        Thread.sleep(4000);
        mouseAxisPosition();
      } // for_2
  } // for_1
 } // storeMouseLocation
} // MouseAxis
Community
  • 1
  • 1
Shantanu Nandan
  • 1,438
  • 8
  • 30
  • 56
  • 2
    Well you never assign a value to `mouseLocation`, so it has the default value of `null`... this wouldn't work with *single*-dimensional arrays either... – Jon Skeet Jul 30 '14 at 16:51

2 Answers2

1

The issue is that you're trying to store data in the indices. If you want an array of locations, then you have a few options:

Have 2 arrays

int lastLocation = 0;
int[] x = new int[10];
int[] y = new int[10];

PointerInfo pointer = MouseInfo.getPointerInfo();
Point point = pointer.getLocation();
x[lastLocation] = (int)point.getX();
y[lastLocation] = (int)point.getY();
lastLocation++;

Given an index i, you can get the x and y values from the array.

Create an array of a specialized type

Just store the whole Point object:

int lastLocation = 0;
Point[] points = new Point[10];
PointerInfo pointer = MouseInfo.getPointerInfo();
Point point = pointer.getLocation();
points[lastLocation] = point;
lastLocaiton++;
bstempi
  • 2,023
  • 1
  • 15
  • 27
0

In Java, an int[][] represents an array of references to arrays of integers. While some languages have a concept of an NxM array of integers, Java does not; the closest it can come would be an array of N references which independently happen to identify distinct M-item arrays of integers.

If one wants to make MouseLocation appear to identify an NxM array of integers, one must first initialize it to identify an array of N references to integer arrays, and then initialize each element of that array to identify a different array of M integers. For example, to initialize it to 5x7, one would do something like:

mouseLocation = new int[5][];
for (int i=0; i<5; i++)
  mouseLocation[i] = new int[7];

Note that Java does not enforce any requirement that all the inner arrays be distinct, but things will behave strangely if they are not. For example, if one says:

mouseLocation[2] = mouseLocation[3];

that may appear initially to copy everything from row 3 to row 2, but it will have another effect as well: any operation on the array identified by mouseLocation[2] will also operate on the array identified by mouseLocation[3], since they will be the same array.

Arrays of arrays can be tricky in Java; in some cases, it may be simpler to represent an NxM grid using a single array of N*M elements. The accessing code may be less intuitive (an access to the element at [i,j] would be written as theArray[i*M+j]) but the array will behave as a single object, rather than as a group of references to objects which should all distinct.

supercat
  • 77,689
  • 9
  • 166
  • 211
  • -1 because Java provides a syntax for setting up a 2-D array, `new int[5][7]`, contrary to your implication that you have to write the loop yourself. +1 for the other useful information. – ajb Jul 30 '14 at 17:17
  • @ajb: I thought I tried that once and it didn't work; did it always provide that feature? I can certainly edit the post to correct that detail. In any case, even if Java provides a syntax equivalent to the loop, it's still important to note that it doesn't create a 5x7 array, but creates an array of five references to seven-element arrays. Am I remembering right that calling methods like `clone` on such an array will thus generally be pretty useless, since the new array will identify the same five seven-element arrays as the original? – supercat Jul 30 '14 at 17:40
  • Yes, I think Java has always had this. `Arrays.copyOf()` can be used to copy a one-dimensional array, but if the elements are references, they will be the same references. I don't see a "deep copy" for multi-dimensional arrays either in the JRE or Apache Commons, but someone says they have one [here](http://stackoverflow.com/questions/1564832/how-do-i-do-a-deep-copy-of-a-2d-array-in-java). – ajb Jul 30 '14 at 17:53