I am trying to fill with asterisks only the outside border of a 2D Array I have half of it done, but it seems that I cant get it to fill the last column and the last row in the 2D array.
so far I can do this
and here is my code:
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array.length; j++)
{
if (array[i][j] == array[0][j] || array[i][j] == array[i][0])
{
array[i][j] = "*";
}
}
}
but obviously I want to finish the Square shape around the 2D array, so I tried something like this.
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array.length; j++)
{
if (array[i][j] == array[array.length - 1][j]
|| array[i][j] == array[i][array.length - 1])
{
array[i][j] = "*";
}
}
}
My idea was just to go to the last valid position in the 2D array and simply print the column and the row but it doesn't seem to work. Thanks to all the help I can get, I really appreciate it as I'm a learner in Java.
#Ricki, your line of thinking was right, but what you didn't consider is that array[i][j] == array[array.length - 1][j] doesn't compare the "shell" per say, but the inner value of it, so, even if array[1][1] != array[2][1], if their values are null they are equals.
Try using this code:
int _i = 10;
int _j = 10;
String[][] array = new String[_i][_j];
for (int i = 0; i < _i; i++) {
for (int j = 0; j < _j; j++) {
if(i==0 || j == 0 || i == _i-1|| j == _j-1){
array[i][j] = "*";
}
}
}
What i've done is comparing the first row (i==0), the first column (j==0), the last row (i == _i-1) and the last column (j == _j-1).
And then:
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
you can do something likewise,
public static void main(String[] args) {
int n = 5;
String [][] array = new String[n][n]; // 2-dimension array define...
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if(i == 0 || (i == array.length-1 || j==0 || j==array.length-1)){ // if top,left,right,bottom line then this...
array[i][j] = "*|";
}else{ // if not border line then this...
array[i][j] = "_|";
}
}
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println("");
}
}
OUTPUT :
Related
Just a simple question. I know to print all possible pairs, you do a nested for loop with a few more adjustments to be fancier. In my case, I want to take it a step up where pairs are not allowed to repeat or have 2 of the same integer in a pair.
For example: (0,0) is not allowed, but (0,1) is allowed. If (0,1) is a pair, then (1,0) is not allowed.
If I had integers "0,1,2,3", then my output would be
(0,0)(0,1)(0,2)(0,3)(1,2)(1,3)(2,3)
This is my current code that won't print pairs with 2 same integers, but repeated pairs still print.
for(int a = 0; a < 4;a++) {
numbers.add(a);
}
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(i != j) {
System.out.println("("+i+" , "+j+")");
}
}
}
Thanks
for(int i = 0; i < 4; i++) {
for(int j = i; j < 4; j++) {
if(i != j) {
System.out.println("("+i+" , "+j+")");
}
}
}
or
for(int i = 0; i < 4; i++) {
for(int j = i + 1; j < 4; j++) {
System.out.println("("+i+" , "+j+")");
}
}
for the given input I need to print the pattern. For example for input = 6 I have to print:
MMMMMMSDDDDDD
MMMMMSSSDDDDD
MMMMSSSSSDDDD
MMMSSSSSSSDDD
MMSSSSSSSSSDD
MSSSSSSSSSSSD
CSSSSSSSSSSSK
CCSSSSSSSSSKK
CCCSSSSSSSKKK
CCCCSSSSSKKKK
CCCCCSSSKKKKK
CCCCCCSKKKKKK
I have tried but couldn't go further than this could anyone help
public class tgk {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int half = ((2*size)+1)/2;
for (int i = 0; i < size ; i++)
{
for (int j = size; j > i; j--)
{
System.out.print("M");
}
for (int k = half+1 ; k > half - i; k--)
{
System.out.print("S");
}
System.out.println();
}
for(int i = size; i > 0; i--)
{
for (int j = size; j >= i; j--) {
System.out.print("C");
}
for (int k = 0; k < (i * 2 - 1); k++) {
System.out.print("S");
}
System.out.println();
}
}
}
if input = 3 it should be
MMMSDDD
MMSSSDD
MSSSSSD
CSSSSSK
CCSSSKK
CCCSKKK
You can use two sets of for loops to print each half of the pattern. Assuming input variable holds the size of the problem
int input = 3;
for (int i = 0; i < input; i++) {
for (int j = 0; j < input - i; j++) {
System.out.print('M');
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print('S');
}
for (int j = 0; j < input - i; j++) {
System.out.print('D');
}
System.out.println();
}
for (int i = input - 1; i >= 0; i--) {
for (int j = 0; j < input - i; j++) {
System.out.print('C');
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print('S');
}
for (int j = 0; j < input - i; j++) {
System.out.print('K');
}
System.out.println();
}
will print for input = 3:
MMMSDDD
MMSSSDD
MSSSSSD
CSSSSSK
CCSSSKK
CCCSKKK
and for input = 6:
MMMMMMSDDDDDD
MMMMMSSSDDDDD
MMMMSSSSSDDDD
MMMSSSSSSSDDD
MMSSSSSSSSSDD
MSSSSSSSSSSSD
CSSSSSSSSSSSK
CCSSSSSSSSSKK
CCCSSSSSSSKKK
CCCCSSSSSKKKK
CCCCCSSSKKKKK
CCCCCCSKKKKKK
I don't know why, but I really wanted it to work with only one set of for-loops:
int number = 8;
for (int i = 0; i < number * 2; i++) {
for (int j = 0; j < (number * 2) + 1; j++) {
System.out.print(
i < number && j+i < number ? 'M' :
i < number && j-i > number ? 'D' :
i < number ? 'S' :
i >= number && i-j >= number ? 'C' :
i >= number && j+i >= number*3 ? 'K' :
'S'
);
}
System.out.println();
}
So for 8 (like in the code) it prints:
MMMMMMMMSDDDDDDDD
MMMMMMMSSSDDDDDDD
MMMMMMSSSSSDDDDDD
MMMMMSSSSSSSDDDDD
MMMMSSSSSSSSSDDDD
MMMSSSSSSSSSSSDDD
MMSSSSSSSSSSSSSDD
MSSSSSSSSSSSSSSSD
CSSSSSSSSSSSSSSSK
CCSSSSSSSSSSSSSKK
CCCSSSSSSSSSSSKKK
CCCCSSSSSSSSSKKKK
CCCCCSSSSSSSKKKKK
CCCCCCSSSSSKKKKKK
CCCCCCCSSSKKKKKKK
CCCCCCCCSKKKKKKKK
...or for 3:
MMMSDDD
MMSSSDD
MSSSSSD
CSSSSSK
CCSSSKK
CCCSKKK
I seem to not be able to solve this java.lang.ArrayIndexOutOfBoundsException: 5
I understand the error, but the table is 5x5 and I think I have everything right for printing it.
public static int tulosta_matriisi(int[][] matriisi) {
int i=0, j=0;
for(i = 0; i <= 4; i++) {
for(j = 0; j <= 4; j++) {
if(j == 4 && i <= 4)
System.out.println(matriisi[i][j]);
else if(i <= 4 && j <= 4)
System.out.print(matriisi[i][j] +"\t");
}
}
return matriisi[i][j];
}
To avoid all this problem you have to use :
for(i = 0; i < matriisi.length; i++) {
for(j = 0; j < matriisi[i].length; j++) {
...
When you get out your loop, the i and j will be incremented so you should not return matriisi[i][j] this make this error java.lang.ArrayIndexOutOfBoundsException: 5, so instead you should to return matriisi[i-1][j-1] so in the end your program should look like this :
public static int tulosta_matriisi(int[][] matriisi) {
int i = 0, j = 0;
for (i = 0; i < matriisi.length; i++) {
for (j = 0; j < matriisi[i].length; j++) {
if (j == matriisi[i].length - 1 && i <= matriisi.length) {
System.out.println(matriisi[i][j]);
} else if (i <= matriisi.length && j <= matriisi[i].length) {
System.out.print(matriisi[i][j] + "\t");
}
}
}
return matriisi[i - 1][j - 1];
}
Good luck
public static void main(String[] args) {
// TODO code application logic here
int numRows = 5;
int numCols = numRows;
int[][] twoDimArray = new int[numRows][numCols];
Random randGen = new Random();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
int randIndex = randGen.nextInt(4);
int value = randGen.nextInt(100);
twoDimArray[i][j] = value;
}
}
System.out.println("\nThe two-dimensional array: ");
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
System.out.print(twoDimArray[i][j] + " ");
}
System.out.println();
}
}
}
I want to find a local minimum using a "brute force" approach. I know with a one dimensional array I would use a for-loop to compare all the elements in the array until I found a local minimum, but I don't know how to do that here.
Edit: Could I use binary search instead? Find the middle row and search there and if one isn't found, I search one of the halves.
The brute force method would be very similar to that of a 1D array, just with an extra loop, and a few more checks:
public int[] findLocalMinimum(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
int current = arr[i][j];
if (i + 1 < arr.length && current >= arr[i + 1][j] ||
i - 1 >= 0 && current >= arr[i - 1][j] ||
j + 1 < arr[i].length && current >= arr[i][j + 1] ||
j - 1 >= 0 && current >= arr[i][j - 1]) {
continue;
} else {
return new int[] { i, j };
}
}
}
return new int[] { -1, -1 };
}
I have an array:
Cell[][] cells = new Cell[width+2][height+2];
Which is filled according to a certain input:
for (int i = 1; i < cells.length-1; i++) {
for (int j = 1; j < cells[i].length-1; j++) {
if (certain input) {
cells[i][j] = new Cell(true);
} else {
cells[i][j] = new Cell(false);
}
}
}
Now I still need to define the border cells which need to become false. I tried this with another for loop but somehow this does not seem to work.
Any help is appreciated!
Don't know if I understand you, but if you want to make a frame for the true cells you should do something like:
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
if (i== 0 || i == cells.length-1 || j== 0 || j == cells.length-1) {
cells[i][j] = new Cell(false);
}
}
}