I am implementing my own Image Convolution method in Java, it is supposed to be general so I can run any kernals through it. It works and I am able to output things, but the output is wrong. The right side of the image appears on the left hand side, and the image seems to triplicate throughout it with varying intensities. This occurs regardless of the kernal I am running (I have tried 9x9 and standard 3x3) and very similar things occur. I have played with this and changed everything to get it to work but it is still behaving incorrectly.
for (int i = 0; i < (image.getWidth()-mask.length); i++)
{
for (int j = 0; j < (image.getHeight()-mask.length); j++)
{
int sum = 0;
for (int w = 0; w < mask.length; w++)
{
for (int z = 0; z < mask.length; z++)
{
sum += arr[w + i][z + j] * mask[w][z];
}
}
if(sum < 170)
{
convArray[i][j] = 0;
}
else
{
convArray[i][j] = 255;
}
Related
I have created a for loop to produce a grid map. When I click each grid on the map I get X and Y of the grid. When map width is greater than map length, everything is fine, but when attempt to create a map where length is greater than with, the returned x becomes the y, and y becomes the x. The issue is at the second for loop when creating the map but I cannot figure it out.
if(mapWidth>mapLength) {
for (int i = 0; i < mapWidth * mapLength; i++) {
y = i / mapLength;
for(int j=0; j<i+1; j++) {
x = j % mapLength;
}
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
} else if(mapWidth<mapLength) { //problematic map is created after this condition
for (int i = 0; i < mapWidth * mapLength; i++) {
x = i / mapLength;
for(int j=0; j < i+1; j++){
y = j % mapLength;
}
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
}
Maps look like this:
Well, maybe I didn't understand well what you expect but I don't think you have to make a special case for the case where mapWidth< mapLength.
Furthermore, I don't get what you intend to do with your nested loop except using CPU resources?
for(int j=0; j<i+1; j++){
x = j % mapLength;
}
When it is left, you will always have x = i % mapLength
Furthermore, as flkes suggested, why don't you use nested loops?
for (int y=0; y < mapLength; y++) {
for(int x=0; x < mapWidth; x++){
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
}
Could you try this code:
for (int i = 0; i < mapWidth * mapLength; i++) {
y = i / mapLength;
x = i % mapLength;
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
I'm writing code for a GoFish game and everytime I get four of a kind I get an out of bounds error.
Here is what I have. I need to find four and then remove it. What I am trying to do with this code is go through each card and check if there is four of it in my hand.
int c = 0;
for (int k = 0; k < hand.size(); k++) {
c = 0;
for (int z = 0; z < hand.size(); z++) {
if(hand.get(k).getRank().equalsIgnoreCase(hand.get(z).getRank())) {
c++;
if (c==4) {
for(int m = z; z > m-4; z-- ) {
hand.remove(m);
}
}
}
}
}
Hi all my program consist of an 2 Dimension array,im reading 2 cordinates in a loop and triying to check if those cordinates in the array are alredy been filled with a asterisc,if this is true y want to re-enicialize my array with the default value "-", and if there is not an asterisc in that specified position y want to fill it in with a asterisc,im not sure if im going for the correct aproach.
this is part of my code.
thanks all.
String[][] matrix = new String[5][5];
String asterisc = "*";
String defaultValue = "_";
Scanner sc = new Scanner(System.in);
int a, b;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
matrix[i][j] = defaultValue;
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
System.out.print(matrix[i][j] + "|");
}
System.out.println();
}
a = 0;
b = 0;
while (a >= 0 && b >= 0 && a < matrix.length && b < matrix.length) {
a = sc.nextInt();
b = sc.nextInt();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
if (matrix[a][b].equals(asterisc)) {
matrix[i][j] = defaultValue;
} else {
matrix[a][b] = asterisc;
}
}
}
}
There are unfortunately many things wrong with your code.
Also you have not explained what your algorithm is trying to do.
In brief, to set all asterisks to defaults, you can do
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if matrix[i][j].equals(asterisc){
matrix[i][j]=defaultValue;
}
}
System.out.println();
}
But
why are you using a while loop?
Why are you using a scanner?
Why are a and b initialised at zero, yet need to be greater than zero for the loop?
Are you really trying to re-initialise your whole array, every time the (a,b) item is asterisc?
I think it is not true.Suppose you have typed "6 6" in the terminal,then the variable a=6,and b=6,which is greater than the array length,and the program will throw a exception.I think the thing you may want to do can follow this codeļ¼
while(true){
a = sc.nextInt();
b = sc.nextInt();
if(a<0||a>matrix.length||b<0||b>matrix.lenght)
break;
}
I have an x by y board of numbers stored in a two dimensional array in java that I would like to print out to the user in a nice formatted manner. Would there be an easy way to do this in java?
Here is how I build my board:
board = new int[TotRow][TotCol];
You might prefer the one line, because it's easy, Arrays.deepToString(Object[]) the Javadoc says (in part),
This method is designed for converting multidimensional arrays to strings.
System.out.println(Arrays.deepToString(board));
If you want the number to be padded properly as well use String.format(),
for (int i = 0; i < TotRow; i++) {
for (int j = 0; j < TotCol; j++) {
System.out.print(String.format("%3d", board[i][j]) + "\t");
}
System.out.println();
}
Notice that I have used %3d for formatting, you can use format as required by you
You can print the board two dimensional array like this
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j] + "\t");
}
System.out.println();
}
Since I don't like relying on tabs, I'd like to mention some approaches that don't rely on them.
If you know that all of the numbers in the table are nonnegative numbers of, say, 4 digits or less, you can use String.format in a manner similar to Nitin's answer:
for (int i = 0; i < TotRow; i++) {
for (int j = 0; j < TotCol; j++) {
System.out.print(String.format("%4d ", board[i][j]));
}
System.out.println();
}
The %4d means to format the number using a width of 4 characters, and pad with blanks on the left if the number takes fewer than 4 characters to print. Thus, if there aren't any 5-digit or longer numbers, each print will print exactly 5 characters--a 4-character field for the number, and a space after the number. This will make everything line up, as long as none of the numbers is too large. If you do get a number that takes more than 4 characters, the formatting will get messed up.
Here's a way to adjust the column width so that it's just large enough to hold all the values in the table, and works fine if there are negative numbers:
int maxWidth = 0;
for (int i = 0; i < TotRow; i++) {
for (int j = 0; j < TotCol; j++) {
maxWidth = Math.max(maxWidth, Integer.toString(board[i][j]).length());
}
}
String format = "%" + maxWidth + "d ";
for (int i = 0; i < TotRow; i++) {
for (int j = 0; j < TotCol; j++) {
System.out.print(String.format(format, board[i][j]));
}
System.out.println();
}
Note: not tested
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(board[i][j] + "\t");
}
System.out.println();
}
You basically just use a nested loop.
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
I am implementing some algorithm, in which the number of loop nested levels is determined by the input.
For example, if the input is 2-dimensional, then there two nested for-loops, as below:
for(int i=0; i<N; i++) {
for(int j=i+1; j<N; j++) {
if(table[i][j] == -1) {
for(int c=0; c<C; c++) {
int ii = table[i][c];
int jj = table[j][c];
sort(ii, jj);
if((T[ii][jj] != -1 && T[ii][jj] < l)) {
T[i][j] = l;
break;
}
}
}
}
}
If the input is 3-dimensional, then it would be something like below:
for(int i=0; i<N; i++) {
for(int j=i+1; j<N; j++) {
for(int k=j+1; k<N; k++) {
if(table[i][j][k] == -1) {
for(int c=0; c<C; c++) {
int ii = table[i][c];
int jj = table[j][c];
int kk = table[k][c];
sort(ii, jj, kk);
if((T[ii][jj][kk] != -1 && T[ii][jj][kk] < l)) {
T[i][j][k] = l;
break;
}
}
}
}
}
If there are only these two case, then I can write two versions of nested for-loops. But the dimensions of input could be any value between 2 and N. In this case, how to control the nested loop level dynamically, or is there any alternative to go around of this?
The only real way to do this is to use recursion.
You write a method containing a single for loop, each time around the loop if it needs to go deeper then the method calls itself with the right settings for that nested loop to be run.
Recursion is already been explained here. However, there is another solution as well. Using only one big loop containing a tiny inner loop.
int n = ...;
int dim = ...;
// Raise n to the power of dim: powN = n^dim
long powN = 1;
for (int i = 0; i < dim; ++i) powN *= n;
int[] indices = new int[dim];
for (long i = 0; i < powN; ++i)
{
// Calculate the indices
long bigI = i;
for (int k = 0; k < dim; ++k)
{
indices[k] = bigI % n;
bigI /= n;
}
// Now all your indices are stored in indices[]
}
I was suggesting something like this :
public static void recursiveLoop(int N, int level, int a){
if (level<0)
return;
for (int i=a; i<N; i++){
System.out.println("Level is : "+ level+ " i: "+i );
recursiveLoop(N,level-1,i+1);
}
}
You may explain what you really want to do.
If the Outer for loops are doing nothing but controlling a count, then your Nested for loops are simply a more complicated way of iterating by a count that could be handled by a Single for loop.
like:
for (x = 0; x < 8; x++) {
for (y = 0; y < 10; y++) {
for (z = 0; z < 5; z++) {
DoYourStuffs();
}
}
}
Is equivalent to:
for (x = 0; x < 8*10*5; x++) {
DoYourStuffs();
}