A while ago before I got used to object object oriented programming I created a basic TicTacToe game and to create the board I used an array.
The code is a complete mess because I didn't properly understand how to use objects, but I did initialize the board correctly:
char[][] board = new char[3][3];
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board[i].length; j++){
board[i][j] = '[]' //or something like that...don't remember exactly
}
}
My question is how would you this with an ArrayList?
ArrayList <ArrayList<Character>> board = new ArrayList(); // this initialization is not
// wrong using Java 8 but for earlier versions you would need to state the type on both
//sides of the equal sign not just the left
for (int i = 0; i < board.size(); i++){
for (int j = 0; j < board.get(i).size(); j++){
board.get(i).get(j).add('[]');
}
}
but that does not work.
It does not have to be exactly like this, I just generally want to understand how to handle multidimensional ArrayLists.
-thanks
Unlike arrays, you can't initialize an entire ArrayList directly. You can specify the expected size beforehand (this helps performance when you are using very large lists, so it is a good practice to do it always).
int boardSize = 3;
ArrayList<ArrayList<Character>> board = new ArrayList<ArrayList<Character>>(boardSize);
for (int i = 0; i < boardSize; i++) {
board.add(new ArrayList<Character>(boardSize));
for (int j = 0; j < boardSize; j++){
board.get(i).add('0');
}
}
The main difference is that in your original code you had a multi-dimensional array of primitives (in this case, char) and all you had to do was assign a new primitive value to each slot in the array.
However what you want now is an ArrayList of (ArrayList of Character). When you create the ArrayList it is empty. In order to procede you are going to need to fill it with several (ArrayList of Character) before you can begin to start adding Characters themselves.
So for example,
ArrayList <ArrayList<Character>> board = new ArrayList<>();
for (int i=0; i<3; i++) {
board.add(new ArrayList<Character>());
}
Now you can start adding Characters to your lists:
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
board.get(i).add('A');
}
}
Hope this helps.
First you have to initialize the ArrayList in your first line correctly and than you have to initialize an new ArrayList in each run of your first loop:
ArrayList <ArrayList<Character>> board = new ArrayList<ArrayList<Character>>();
for (int i = 0; i < 3; i++){
ArrayList<Character> innerList = new ArrayList<Character>();
board.add(innerList);
for (int j = 0; j < 3; j++){
innerList.add('[');
}
}
Related
int[] box = new int[9*8];
for(int i=0; i<9; i++) {
for(int j=0; j<8; j++) {
box[j] = i;
}
}
I've tried everything and it turns out to be way harder than it looks for me. Without using ArrayLists (I understand this works using box.add(i)) I can only use int[] type. I need to create a list of integers that looks like this [0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,..8,8,8,8,8,8,8,8] so 8 sets of integers from 0-8. Can anyone help me?
I believe the problem is that on line 4. The code sets a position on to a value, but this position repeats from 0 to 7.
This should work better:
int[] box = new int[9*8];
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 8; j++) {
box[i * 8 + j] = i;
}
}
Basically, it shift the 0 - 7 over 8 places for every new number.
So I'm having some trouble with this in java. This method is supposed to take in two String arrays which are multidimensional. This method is supposed to return a a copy String array. Now these two arrays that are being sent to the copy method are going to have randomly generated lengths within the arrays. I am trying to see if array2 is smaller than array and if it is then array will be assigned the length of array2 and I want to copy elements from array into array2. If it is the opposite with array2 being bigger than array then I would like to copy the elements from array into array2 and then the REMAINING spots in array2 will get filled with an *. Now the issue is I keep getting runtime errors with out of bounds for the index. It is specifically the lines that I have indicated with double asterisks below(within for loops) that are giving me issues. What could I do to solve the issue.
public static String[][][] copy(String[][][] array, String[][][] array2){
if(array2.length < array.length){
array = new String[array2.length][array2.length][array2.length];
for(int i = 0; i < array2.length; i++){
for(int j = 0; j < array2[i].length; j++){
for(int k = 0; k < array2[i][j].length; k++){
**array2[i][j][k] = array[i][j][k];**
}
}
}
return array2;
}
if(array2.length > array.length){
for(int i = 0; i < array.length; i++){
for(int j = 0; j < array[i].length; j++){
for(int k = 0; k < array[i][j].length; k++){
**array2[i][j][k] = array[i][j][k];**
}
}
}
for(int i = array.length - 1; i < array2.length; i++){
for(int j = array[i].length - 1; j < array2.length; j++){
for(int k = array[i][j].length - 1; k < array2.length; k++){
array2[i][j][k] = "*";
}
}
}
return array2;
}
return array;
}
You assume that each dimension of the array is of equal size.
But you cannot even assume that two sibling arrays are of equal size.
What I mean is, that array = new String[array2.length][array2.length][array2.length]; is nonsense and at least in two ways.
You probably want to have array2 = new String[array.length][][], because if you reassign array you will just copy nulls around. And for the lengths of the other dimensions you have to do the same comparisons and resizings, especially if you did not reassign the array2. If you did, then you have to create the sub-level arrays afresh anyway.
For school work I need to write a constructor for a class that contains a 2-dimensional array of integers. The constructor copies a passed in two-dimensional array. Below is the code I have so far. The current issue I have is how to initialize the array when the "column" size of the passed in array is unknow. The issue I think I am having is when creating and initializing the array. The length of the inner and out array is unknown.
public IntMatrix (int[][] array)
{_matrix = new int [array.length][array.length-1].length];
for (int i = 0; i < array.length; i++) {
for(int j=0; j < array[i].length; j++)
_matrix[i][j]=array[i][j];
}
}
As I said in a comment, what you have is an array of arrays:
public IntMatrix(int[][] array) {
matrix = new int[array.length][];
for (int i = 0; i < array.length; i++) {
matrix[i] = new int[array[i].length];
for(int j=0; j < array[i].length; j++) {
matrix[i][j] = array[i][j];
}
}
}
You can always determine the size of an array via myArray.length, so you can allocate for each row/column as you iterate through.
A thought, however. Is it acceptable to simply store the reference to the array that you're passed ? Will it change outside your class ? If not, then that might be a simple solution if you don't have to recreate the array internally.
This is my first time asking a question here, so forgive me if my formatting is a little sloppy.
I need to know how to set all the elements of a 2D Array to the same value, using for loops, with Java.
I was able to get this far:
import java.util.Arrays;
public class TheArray{
public static void main (String[] args){
int[][] pixels = new int[768][1024];
for(int i = 0; i < pixels.length; i++){
for(int j = 0; j < pixels[i].length; j++){
Arrays.fill(pixels[i], 867);
}
}
}
}
However I have no idea if I have done it right. So all I want it to do is make all of the elements of the 2D array be 867.
You don't need to use Arrays.fill if you're already using for loops. Just do:
for(int i = 0; i < pixels.length; i++){
for(int j = 0; j < pixels[i].length; j++){
pixels[i][j] = 867;
}
}
Arrays.fill would be used instead of the inner for loop.
Use the following line inside your loop:
pixels[i][j] = 867;
Here is a particular method I have written:
class A {
private static ArrayList<ArrayList<Integer>> inputTerms = new ArrayList<ArrayList<Integer>();
public static void method1(ArrayList<Integer> terms) {
ArrayList<Integer> clauses = new ArrayList<Integer>();
int N = terms.size();
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
clauses.add(-terms.get(i));
clauses.add(-terms.get(j));
inputTerms.add(clauses);
clauses.clear();
}
}
}
}
This method is called multiple times from the main function.
In the end, i try to write the contents of the class variable into a file. However, when I do this, i get 0 as the contents of inputTerms. However, if i remove the clauses.clear() line, i am able to get approppriate values.
My program is such that it is vital for me to clear the clauses after adding to inputTerms. Is there any alternative to this?
**Hmmm.. I have done what you've suggested. However, I haven't quite overcome the problem. To give more background, in my main function, I have the following code:
for (int i=0; i<N-1; i++){
ArrayList<Integer> firstdiagonalTerms = new ArrayList<Integer>();
for (int j=0; j<N-i; j++){
firstdiagonalTerms.add(variable[j][i+j]);
}
method1(firstdiagonalTerms);
}
I have to call the method1 function 4 times for different combinations of 'i' and 'j'. However, I still get 0 when I use the above mentioned suggestions**
You are adding the same list and clearing it repeatedly. When you add an object to a list it copies a reference to it, not a copy of the object.
int N = terms.size();
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
List<Integer> clauses = new ArrayList<Integer>();
clauses.add(-terms.get(i));
clauses.add(-terms.get(j));
inputTerms.add(clauses);
}
}
or
for (int i = 0, N = terms.size(); i < N - 1; i++)
for (int j = i + 1; j < N; j++)
inputTerms.add(Arrays.asList(-terms.get(i), -terms.get(j)));
Not sure i understand what you are trying to achieve, but you keep reusing the same list, which is probably not what you meant to do.
You should probably move the ArrayList<Integer> clauses = new ArrayList<Integer>(); inside the inner loop, and not call clauses.clear() at all.
When you are adding "clauses" you are adding the actual object to the arrayList, not a copy. So when you clear them all the values in the list will be removed. To get arround this, add a clone of the list:
inputTerms.add((ArrayList<Integer>) clauses.clone());
When you call clear() on list, you are updating/removing same objects (because list contains reference to objects, not copy of object). That is what causing the issue.
I think you need to do something like below. Instead of using clear(), create a new list everytime.
public static void method1 (ArrayList<Integer> terms)
{
int N = terms.size();
for (int i = 0; i<N-1; i++) {
for (int j=i+1; j<N; j++) {
ArrayList<Integer> clauses = new ArrayList<Integer>();
clauses.add(-terms.get(i));
clauses.add(-terms.get(j));
inputTerms.add(clauses);
}
}