449,220 Members | 1,723 Online Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

 P: 12 Hey guys, I hate to bother you all, but I had a HW assignment last week that I couldn't get no matter what values (variables) I tried and according to my professor, it's too easy to have to go over in class (I don't think so), so even though I failed the homework, I have no idea on "the right way to do it" and I'm worried that I might be faced with a similar problem on the test in October. Any help in assisting me with the search and delete methods in this class would be greatly appreciated. Expand|Select|Wrap|Line Numbers public class ArrayNew    {       private double[][] b;       public ArrayNew(int x, int y)       {          b = new double[x][y];         }          public void insert (int x, int y, int num)       {          b[x][y] = num;         }              public boolean find(int searchKey)       {            int row = 0;             int column = 0;          for (row = 0; row < b.length-1; row++)             {                 for (column = 1; column < b[row].length; column++)                                   if(b[row][column] == searchKey)                   break;                             }           if(row == b.length-1 && column == b[row].length)              return false;          else             return true;           }            public boolean delete (int value)       {          int i = 0;          int j = 0;          for (i = 0; i < b.length-1; i++)             for (j = 0; j < b[i].length; j++)                      {                               if (b[i][j] == value)                   break;                    }                   if (i == b.length-1 && j == b[i].length )             return false;          else          {             for (int k = i; k < b.length-1; k++)                             for (int h = j; h < b[k].length; h++)                   {                                     b[k][h] = b[k+1][h+1];                            }                        return true;               }          }        public void display()       {          for (int i = 0; i < b.length; i++)             {             for (int j = 0; j < b[i].length; j++)             {                        System.out.print(b[i][j] + " ");                   }                    System.out.println("");         }          }      } The original assignment was to create a 2d array using search, delete, and insertion methods, duplicates were not to be considered. Now I've gotten search to where it will show values from the 2nd row onward, but it will not show any values from the first row. Delete will not work at all. Oh and if there is a way to do either method without a nested for loop, please say something, I don't want you to do it for me, just give me a hint or two. Thanks in advance Sep 2 '08 #1
3 Replies

 100+ P: 188 Hi there, When searching do not initialise column in the inner loop to 1. That will skip the value at index  which is not what you want to do. Just do it like in the outer loop and remember Expand|Select|Wrap|Line Numbers  b.length() -1  . In the delete part you are breaking out of the inner loop but the outer one is still on. this ruins the i and j counters so they are equal to Expand|Select|Wrap|Line Numbers  b.length() -1  and it therefore returns false. Try stepping through the code or print out the values in the variables as you go. Regards Alex. Sep 3 '08 #2

 P: 12 Thank you very much, that helped a lot! Sep 4 '08 #3 