Dylan Dylan - 2 months ago 7
Java Question

Classes and 2 Dimension Arrays

The code I am working with is below. I feel like this should be simple, but I'm having an incredibly hard time focusing this week and need some help.

I'm not able to properly set the values for pt.x or pt.y in the nested for loops. IDEA is telling me that the symbol can't be resolved. The class is identified from another java file in the package that only identifies that class. it is as follows:

public class pointClass {
class point{
int x;
int y;
int z;
}
}


(Adding text to demonstrate these are 2 separate files)

This is for a class assignment, but I'm not sharing the whole assignment, just what I need help with. I'm trying to learn, not have things done for me.

public class main {
public static void main (String args[]){

ArrayList<pointClass.point> pointlist = new ArrayList<>();

//Creating map
int row = 40;
int col = 40;
int [][] bigarray = new int [row] [col];

//iterating through map
for (int i = 0; i < row; i++;){
for (int j=0; j< col; j++){
pointClass pt = new pointClass.point;
pt.x = row;
pt.y = col;
pt.z = ;//RNG HERE//

}
}


How do I need to more properly identify these class attributes? For context,this code cretes a 40x40 array and will assign a random value to each number. Another code stanza will be added to print the 2D array.

Answer

There doesn't seem to be a need for a Nested class here. Consider just using the following:

public class Point {
    int x;
    int y;
    int z;
}

Now let's take a look at your syntax errors. Most are fairly simple, but deserve discussion nonetheless.

public class Main {
    public static void main(String args[]){

        ArrayList<Point> pointlist =  new ArrayList<>(); //Now that we aren't using a nested class, Just <Point>            

        //Creating map
        int row = 40;
        int col = 40;
        int [][] bigarray = new int [row] [col];

        //iterating through map
        for (int i = 0; i < row; i++){ //No semicolon after i++
            for (int j=0; j< col; j++){
                Point pt = new Point(); //Calling a constructor is a method, hence ()
                pt.x = j; //You probably mean j and k here, not row and col (which don't change)
                pt.y = k;
                pt.z = 0;//RNG HERE// //Not sure what you mean here, but you can set pt.z to whatever you want

                //You created pointlist, but never add to it. Did you mean to?
                pointlist.add(pt);
            }
        }
    }
}

I've just tested the above and it compiles and runs correctly. That said, you can do a lot better stylistically. Here are some tips.

  • Class names start with a capital letter. Point, not point. PointClass, not pointClass.
  • Non-final / mutable fields should be private. Thus your Point class, while correct, is fairly bad practice (the reasons for which are very well documented elsewhere). Consider using the following alternative:

    public class Point {
        private int x;
        private int y;
        private int z;
    
        public Point(int x, int y, int z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
    
        public int getX() { return x; }
        public int getY() { return y; }
        public int getZ() { return z; }
    }
    
Comments