0

Here's a piece of my code. I have a problem with the insertPoint method. I have an array that I need to name. In insertPoint method I give a String variable that gives the name to the method. But it won't take the name complaining "the type of the expression must be an array type but it resolved to string". I tried adding "Node name[]" but it gave me an error saying "dublicate local variable name". Not sure how to resolve this problem.

public class Node {

    String name;

    public void newArray(int number) {
        this.name = "children" + number;
        final Node name[]  = new Node[4]; // Node's name should now be e.g. "children0"
        insertPoint(this.x, this.y, size, this.name);
    }

    public void insertPoint(int x, int y, int size, String name) {
        // Node name[]; // dublicate local variable name
        if (this.x < c) {
            if (name[0] == null) { // the type of the expression must be an array type but it resolved to string
                name[0] = new Node(x, y, 0, 0, length);
            } else {
                newArray(0);
            }
        }
    }

}
user2222
  • 3
  • 3
  • I don't quite understand what you're trying to do with having two variables named `name`, one a String (typical for names), and one an array of `Node` objects. – Vitruvie Dec 26 '14 at 10:08
  • You cannot "name (= give a name to, baptize) an array". Arrays are objects without a name; a reference to an array is stored in a variable which has a name (in your source program). Please post more of your class Node and describe what it is all about. – laune Dec 26 '14 at 10:09
  • I'll try to explain. The newArray method makes a new array of Node objects (e.g. Node children2[] = new Node[4]). In the insertPoint method I'm trying to look if children2[] arrays first element children2[0] is empty. There can be many arrays of Node objects so I can't just name them "myself", the code is supposed to add a number to make the names different. – user2222 Dec 26 '14 at 10:12
  • What's the point of just making a new array? You don't store it anywhere and it gets thrown out with the garbage. – Vitruvie Dec 26 '14 at 10:13
  • Well the array is supposed to remember the added points which are coordinates (x and y). – user2222 Dec 26 '14 at 10:16
  • That's not how this works. That's not how any of this works. You need to start over and read tutorials on what exactly an Object is and how to make things happen in Java. Try http://docs.oracle.com/javase/tutorial/java/TOC.html . You need to understand how to store data and how methods and variables work. – Vitruvie Dec 26 '14 at 10:22

1 Answers1

0

Java Doesn't support dynamic variable declaration or change. in your code you are trying to name Node name as children0. Java compiler doesn't change the variable name.

        this.name = "children" + number;
        final Node name[]  = new Node[4];

You can use Map to store dynamic variable as keys and values.

Map mMap = new HashMap();
mMap.put("children0",your any value);  //to insert
mMap.get("children0");//to get back
Bruce
  • 8,609
  • 8
  • 54
  • 83
  • Oh, that's good to know. Is there any other way for me to do this then? Because the name of the array needs to be changed so that I know in which node (place) the point (coordinates) are located at. I'm trying to build up a quadtree. – user2222 Dec 26 '14 at 10:21
  • Use Java HashMap and put each entry with key value pair – Bruce Dec 26 '14 at 10:23
  • That's one idea. I was also thinking would two-dimensional array work here as well? – user2222 Dec 26 '14 at 10:27