I have a problem that, whenever I try to fill two dimensional arrayList (5x5) with one dimensional, I get index out of bounds exceptions. I guess that's because the square Array doesn't have index 0 value yet, but I don't know how to fix it.
ArrayList<Character> c = new ArrayList<>();
// Copy character by character into arraylist
for (int i = 0; i < finalArray.length(); i++) {
c.add(i, finalArray.charAt(i));
}
ArrayList<ArrayList<Character>> square = new ArrayList<>();
//square.add(new ArrayList<>());
int k = 0;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
square.get(j).add(i, c.get(k));
k++;
}
}
Add a “row” List to the outer List before the inner loop. You are also using the wrong index for square.
int k = 0;
for(int i = 0; i < 4; i++){
square.add(new ArrayList<>()); // Initialize the row
for(int j = 0; j < 4; j++){
square.get(i).add(c.get(k++)); // get(i) not get(j)!
}
}
Note the other simplifications too.
It would be clearer to do this:
int k = 0;
for(int i = 0; i < 4; i++){
List<Character> row = new ArrayList<>();
square.add(row);
for(int j = 0; j < 4; j++){
row.add(c.get(k++));
}
}
Related
I have to work on a project in my project I have to find TicTacToe game winner.
String [][]board=new String [10][10];
Where are 10 rows and 10 columns
I can find winner horizontally:
for (int I=0;I<10;I++){
String line1=new String();
for (int i=0;i<10;i++) {
line1+=index[I][i];
}
if (line1.equals("XXXXXXXXXX")) {
System.out.println("winner is X");///return here X
} else if (line1.equals("OOOOOOOOOO")) {
System.out.println("winner is O");//return here O
}
}
}
But I am not able to find a winner vertically, I have 1 option I can
hard code it but I want to code it logically like my code for
horizontally.
I have solved this problem, first rotate the array then again check using horizontally code
//first find the transpose of the index.
for (int i = 0; i < 10; i++){
for (int j = i; j < 10; j++){
String temp = index[i][j];
index[i][j] = index[j][i];
index[j][i] = temp;
}
}
//reverse each row
for (int i = 0; i< 10; i++){
for(int j = 0; j< 10/2; j++){
String temp = index[i][j];
index[i][j] = index[i][10 - 1 - j];
index[i][10 - 1 - j] = temp;
}
}
Now array is rotated and now i can use my Horizontally checking code.
for (int I=0;I<10;I++){
String line1=new String();
for (int i=0;i<10;i++) {
line1+=index[I][i];
}
if (line1.equals("XXXXXXXXXX")) {
System.out.println("winner is X");///return here X
}else if (line1.equals("OOOOOOOOOO")){
System.out.println("winner is O");//return here O
}
}
It's simply first rotate the array then again check using horizontally code
//first find the transpose of the index.
for (int i = 0; i < 10; i++){
for (int j = i; j < 10; j++){
String temp = index[i][j];
index[i][j] = index[j][i];
index[j][i] = temp;
}
}
//reverse each row
for (int i = 0; i< 10; i++){
for(int j = 0; j< 10/2; j++){
String temp = index[i][j];
index[i][j] = index[i][10 - 1 - j];
index[i][10 - 1 - j] = temp;
}
}
rotate the array then again check using horizontally code, it's easy
//first find the transpose of the index.
for (int i = 0; i < 10; i++){
for (int j = i; j < 10; j++){
String temp = index[i][j];
index[i][j] = index[j][i];
index[j][i] = temp;
}
}
//reverse each row
for (int i = 0; i< 10; i++){
for(int j = 0; j< 10/2; j++){
String temp = index[i][j];
index[i][j] = index[i][10 - 1 - j];
index[i][10 - 1 - j] = temp;
}
}
Code:
static void exchangeColumns(int matrix[][])
{
int i;
int n = matrix[0].length;
for (i=0;i<n;i++){
int temp = matrix[i][0];
matrix[i][0] = matrix[i][n-1];
matrix[i][n-1] = temp;
}
}
You are using a wrong way to iterate the multi-dimensional array. Please use the following way to iterate through your array.
for (int i = 0; i < matrix.length; ++i) {
for(int j = 0; j < matrix[i].length; ++j) {
System.out.println(matrix[i][j]); // Here you can place your logic by accessing the array elements
}
}
I have created a 2D array to accept values from users. However, when I ran the code and tried to input values, it's not accepting the values.
int rows = 0;
int cols = 0;
int[][] Array = new int[rows][cols];
Scanner entry = new Scanner(System.in);
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
Array[rows][cols] = entry.nextInt();
}
}
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
System.out.println(Array[rows][cols]);
}
}
The value of rows and columns is 0 and you are creating an array with rows and columns size. So your array will be of size 0 and won't accept any values. Try to change the values of rows and columns.
int rows = 5;
int cols = 5;
int[][] Array = new int[rows][cols]; // new int[5][5];
Scanner entry = new Scanner(System.in);
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
Array[i][j] = entry.nextInt();
}
}
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
System.out.println(Array[i][j]);
}
}
I am using Java and working on 5X5 board game(represented as String[][]) and I looking for an efficient way to randomly place 3 "A"'s, 3 "B",s 3 "C"'s, 3 "D"'s on the board.
I thought about using a nested for loop inside of a while loop to go over each slot, and randomly assign letters to 'slots' on the board, but I want to possibly do it in one pass of the board, if there is a good way to randomly place all 15 letters on the board in one pass.
Any suggestions?
You can use an ArrayList to store the letters (and the empty cells, I'm using a dot . so you can recognize it in the output), then use Collections.shuffle() to put elements of the ArrayList in "random" places. Finally assign each letter to the String[][] array:
public static void main(String[] args)
{
String[][] board = new String[5][5];
List<String> letters = new ArrayList<>();
// fill with letters
for (int i = 0; i < 3; i++) {
letters.add("A");
letters.add("B");
letters.add("C");
letters.add("D");
letters.add("E");
}
// fill with "empty"
for (int i = 0; i < 10; i++) {
letters.add(".");
}
Collections.shuffle(letters);
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
board[i][j] = letters.get(i*board.length + j);
}
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
Output Sample:
C B . . .
A E . E A
. A . D D
C . . . D
B C E . B
Note:
The operation i*board.length + j will generate consequent numbers 0, 1, 2, 3, ... 24 en the nested loop.
One way: create an ArrayList<Character> or of String, feed it the 15 letters, call java.util.Collections.shuffle(...) on the ArrayList, and then iterate over this List, placing its randomized items into your array.
e.g.,
List<String> stringList = new ArrayList<String>();
for (char c = 'A'; c <= 'E'; c++) {
for (int i = 0; i < 3; i++) {
stringList.add(String.valueOf(c));
}
}
for (int i = 15; i < 25; i++) {
stringList.add(null);
}
Collections.shuffle(stringList);
String[][] gameBoard = new String[5][5];
for (int i = 0; i < gameBoard.length; i++) {
for (int j = 0; j < gameBoard[i].length; j++) {
gameBoard[i][j] = stringList.get(i * gameBoard.length + j);
}
}
// now test it
for (int i = 0; i < gameBoard.length; i++) {
for (int j = 0; j < gameBoard[i].length; j++) {
System.out.printf("%-6s ", gameBoard[i][j]);
}
System.out.println();
}
Assume you are given an int variable named nPositive and a 2-dimensional array of ints that has been created and assigned to a2d. Write some statements that compute the number of all the elements in the entire 2-dimensional array that are greater than zero and assign the value to nPositive.
Code:
for(int i=0; i<a2d.length; i++){
int nPositive;
for(int j=0; j<a2d[a2d.length-1].length; j++) {
if(a2d[i][j] > 0) {
nPositive = a2d[i][j];
}
}
}
It has a compilation error. Why?
The iiner cycle is incorrect:
for(int j=0; j<a2d[i].length; j++){
You didn't initialize nPositive.
// make nPositive a global variable
int nPositive = 0;
for(int i=0; i<a2d.length; i++){
for(int j=0; j<a2d[a2d.length-1].length; j++) {
if(a2d[i][j] > 0) {
nPositive += a2d[i][j]; // add the value into nPositive as you go through the array
}
}
}
I tested it and find that,There is no any compilation error in your code...
for(int j=0; j<a2d[a2d.length-1].length; j++){//
let the length is a2d[10][10]
on statement a2d[a2d.length-1].length ,is equal a2d[10-1].length ,is equal a2d[9].length=>10
your algo is working fine for me ,i found no any error
here's my test code
public class A2dTest {
public static void main(String[] arr) {
int[][] a2d = new int[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
a2d[i][j] = (int) (Math.random() * 100) + 1000000;// all positives
}
}
for (int i = 0; i < a2d.length; i++) {
int nPositive = 0;
for (int j = 0; j < a2d[a2d.length - 1].length; j++) {
if (a2d[i][j] > 0) {
nPositive = a2d[i][j];
System.out.println("nPositive=" + nPositive);
}}
}
}
}
I believe this is one of the questions on codeLab. You just need to properly initialize nPositive at 0 and increment it for every positive integer. That's all they're looking for involving the output. So your code needs to be:
nPositive = 0;
for (int i = 0; i < a2d.length; i++)
{
for (int j = 0; j < a2d[i].length; j++)
{
if (a2d[i][j] > 0)
{
nPositive++;
}
}
}