-3

I want to create a simple 2D histogram from array of points.

Class Points

import java.util.ArrayList;
import java.util.List;


public class Points {
    static List<List<Integer>> histogram = new ArrayList<List<Integer>>();

    public static void createHistogram(List<Point> point,int max) {     
        for(int x = 0; x < max; x++) {
            histogram.add(new ArrayList<Integer>());
            for(int y = 0; y < max; y++) {
                histogram.get(x).add(0);
                System.out.print((histogram.get(x).get(y)) +" ");
            }
            System.out.println();
        }

        for(int x = 0; x < point.size(); x++)
            histogram.get(point.get(x).x).get(point.get(x).y)++;
    }

    public static void main(String[] args) {
        List<Point> points = new ArrayList<Point>();

        points.add(new Point(0,10));
        points.add(new Point(1,2));
        points.add(new Point(2,5));
        points.add(new Point(1,2));


        createHistogram(points,10);
    }


}

Point Class

public class Point{
    public int x = 0;
    public int y = 0;

    Point(int x, int y){
        this.x = x;
        this.y = y;
    }
}

I get "Invalid argument to operation ++/--" error when I try to increment the value of histogram. Why is that? When I print the value of "histogram.get(point.get(x).x).get(point.get(x).y)" there is no issue. Why changing its value is not permitted? How can I fix it?

No Name
  • 39
  • 1
  • 8

3 Answers3

2

Why is that?

Because the increment and decrement operators can only be applied to variables (either local variables or class variables) or array elements. You're trying to apply it to the return value of a function call — but the operator has no way of knowing how to write the new value back.

Instead, you need to get the value, add one to it, and set the value through the appropriate setter method.

To be clear, if you're trying to increment the x or y on a Point instance, you can do that with ++:

thePoint.y++;

but histogram.get(point.get(x).x).get(point.get(x).y)++; tries to do it on the return value of get.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1
for(int x = 0; x < point.size(); x++)
        histogram.get(point.get(x).x).get(point.get(x).y)++;

Try

for(int x = 0; x < point.size(); x++) {

    List<Integer> list = new ArrayList<Integer>(histogram.get(point.get(x).x));
    list.set(point.get(x).y, point.get(x).y + 1);
    histogram.set(point.get(x).x, list);
}
M_S_72
  • 79
  • 6
0
public static void main (String[] args)
{
        
        int n=5;
        System.out.println(--++n);
}

I am not sure if I agree with the above code snippet. A simple consecutive increment and decrement throws the same error: Invalid argument to operation.

flyingfishcattle
  • 1,817
  • 3
  • 14
  • 25
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34186197) – Miss Skooter Apr 12 '23 at 19:13
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 13 '23 at 02:33