6 Replies - 4301 Views - Last Post: 04 June 2011 - 11:20 AM Rate Topic: -----

#1 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

How to clear a 2D Array

Posted 03 June 2011 - 08:49 PM

In my program I used a 2D int array and store it for things. At the end of the program, I wanted to reset the array. The problem is that the fill ability in the util class(I think it is a class) is for 1D arrays. I thought of doing an enhanced for loop, but only the first row is cleared out. i am not sure how to clear the entire array.
Is This A Good Question/Topic? 1
  • +

Replies To: How to clear a 2D Array

#2 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: How to clear a 2D Array

Posted 03 June 2011 - 09:04 PM

View PostIpodhero178, on 04 June 2011 - 04:49 AM, said:

In my program I used a 2D int array and store it for things. At the end of the program, I wanted to reset the array. The problem is that the fill ability in the util class(I think it is a class) is for 1D arrays. I thought of doing an enhanced for loop, but only the first row is cleared out. i am not sure how to clear the entire array.


nested for loop?

for (int row = 0; row < ROW.length; row++) {
   for (int col = 0; col < COL.length; col++) {
      array[row][col] = 0;
   }
}


Ok, in order to reset the array you first need to declare the array as a global variable e.g.:
int[][] array;



Then you need a method like this:
public int[][] newArray(int row, int col) {
   return new int[row][col];
}



Then everytime you want to change out the contents of the array just call:
int row = input;
int col = input;
array = newArray(row, col);


This post has been edited by x68zeppelin80x: 03 June 2011 - 09:17 PM

Was This Post Helpful? 1
  • +
  • -

#3 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Re: How to clear a 2D Array

Posted 03 June 2011 - 09:51 PM

View Postx68zeppelin80x, on 03 June 2011 - 10:04 PM, said:

View PostIpodhero178, on 04 June 2011 - 04:49 AM, said:

In my program I used a 2D int array and store it for things. At the end of the program, I wanted to reset the array. The problem is that the fill ability in the util class(I think it is a class) is for 1D arrays. I thought of doing an enhanced for loop, but only the first row is cleared out. i am not sure how to clear the entire array.


nested for loop?

for (int row = 0; row < ROW.length; row++) {
   for (int col = 0; col < COL.length; col++) {
      array[row][col] = 0;
   }
}


Ok, in order to reset the array you first need to declare the array as a global variable e.g.:
int[][] array;



Then you need a method like this:
public int[][] newArray(int row, int col) {
   return new int[row][col];
}



Then everytime you want to change out the contents of the array just call:
int row = input;
int col = input;
array = newArray(row, col);


What did you mean by the coding behind the for loops? I mean the ROW.length, wouldn't it be the array.length?

Also, the array is in a class, so it must be a private variable

This post has been edited by Ipodhero178: 03 June 2011 - 09:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: How to clear a 2D Array

Posted 03 June 2011 - 10:32 PM

View PostIpodhero178, on 04 June 2011 - 05:51 AM, said:

What did you mean by the coding behind the for loops? I mean the ROW.length, wouldn't it be the array.length?


ROW and COL are defined by YOU.
ROW.length is how many integers are stored in the first part of the array: array[n][]
COL.length is how many integers are stored in the second part of the array: array[][n]

View PostIpodhero178, on 04 June 2011 - 05:51 AM, said:

Also, the array is in a class, so it must be a private variable


yes, the array should be private, I was just proving a point, I did not care about encapsulation. If the array is in another class then you should have an accessor/mutator for that array.

private int[][] array;

// Note, setters are usually private or protected, but if you need to explicitly 
// change the array at any time, go ahead and make it public. 
public void setArray(int row, int col) {
   array = new array[row][col]
}

public int[][] getArray() {
   return array;
}


This post has been edited by x68zeppelin80x: 03 June 2011 - 10:33 PM

Was This Post Helpful? 1
  • +
  • -

#5 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Re: How to clear a 2D Array

Posted 03 June 2011 - 11:19 PM

I see. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: How to clear a 2D Array

Posted 04 June 2011 - 07:29 AM

View PostIpodhero178, on 04 June 2011 - 04:49 AM, said:

for (int row = 0; row < ROW.length; row++) {
   for (int col = 0; col < COL.length; col++) {
      array[row][col] = 0;
   }
}


If it's not 2D, but a ragged array instead, this way MIGHT cause a few problems. I prefer doing this:
for (int row = 0; row < array.length; row++) {
   for (int col = 0; col < array[row].length; col++) {
      array[row][col] = 0;
   }


If you ensure it's 2D, then the first way is ok though
Was This Post Helpful? 0
  • +
  • -

#7 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Re: How to clear a 2D Array

Posted 04 June 2011 - 11:20 AM

View PostDogstopper, on 04 June 2011 - 08:29 AM, said:

View PostIpodhero178, on 04 June 2011 - 04:49 AM, said:

for (int row = 0; row < ROW.length; row++) {
   for (int col = 0; col < COL.length; col++) {
      array[row][col] = 0;
   }
}


If it's not 2D, but a ragged array instead, this way MIGHT cause a few problems. I prefer doing this:
for (int row = 0; row < array.length; row++) {
   for (int col = 0; col < array[row].length; col++) {
      array[row][col] = 0;
   }


If you ensure it's 2D, then the first way is ok though


I actually did it that way and it worked fine. Thanks everyone.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1