Java Array Manipulation

前端 未结 3 382
日久生厌
日久生厌 2021-01-27 07:52

I have a function named resize, which takes a source array, and resizes to new widths and height. The method I\'m using, I think, is inefficient. I heard there\'s a better way t

3条回答
  •  离开以前
    2021-01-27 08:09

    Based on your other question, it seems like you may be having trouble with mixing of types - with numeric conversions. One way to do this, which can make your code more debuggable and more readable to others not familiar with the problem space, would be to split the problematic line into multiple lines. Each minor operation would be one line, until you reach the final value. For example,

    newImage[(int)(i*scale+s1)][(int)(j*scale+s2)] =source[i][j];
    

    would become

    int x = i * scale;
    x += s1;
    int y = j* scale;
    y +=s2;
    
    newImage[x][y] = source[i][j];
    

    Now, you can run the code in a debugger and look at the values of each item after each operation is performed. When a value doesn't match what you think it should be, look at it and figure out why.

    Now, back to the suspected problem: I expect that you need to use doubles somewhere, not ints - in your other question you talked about scaling factors. Is the factor less than 1? If so, when it's converted to an int, it'll be 0, and you'll get the wrong result.

提交回复
热议问题