Ordered arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JinFTW
    New Member
    • Feb 2008
    • 12

    Ordered arrays

    Hey guys I hate to bother you with another question so soon, but I've set up a 2d ordered array, and while my delete and search methods work okay, my display/insertion methods have to be missing something, or perhaps I've used too much information. Don't get me wrong, the 2d array in my driver is in ascending order like it should be, but the elements are not in the right coordinates. The only one that is placed right is 0,0. Everything else just shows up in 1,1 or 2,2, or 3,3, or 4,4, instead of where they are supposed to go. Any hints would be greatly appreciated.

    Code:
     class ArrayBinary						
       {
          private int[][] a;		
          private int nElems;		
       
           public ArrayBinary (int x, int y) 
          {
             a = new int[x][y];			
             nElems = 0;
          }
       
           public int size()
          {
             return nElems;					
          }
       
           public int find(int searchKey)
          {
             int lowerBound = 0;
             int upperBound = nElems-1;
             int middle;
          
             while(true)
             {
                middle = (lowerBound + upperBound) / 2;
                if(a[middle][middle] == searchKey)
                   return middle;										//found it
                else if (lowerBound > upperBound)
                   return nElems;	
                else
                {
                   if (a[middle][middle] < searchKey)
                      lowerBound = middle + 1;					
                   else
                      upperBound = middle - 1;					
             }			// ends find
          }		
           public void insert (int x, int y, int value)		
          {
           int i;														
             for (i =0; i < nElems; i++)			
                if(a[i][i] > value)
                   break;
             for (int j = nElems; j > i; j--)
                a[j][j] = a[j-1][j-1];			
             a[i][i] = value;					
             nElems++;							
          }
       		
       
       		
           public boolean delete(int value)
          {
             int i = find(value);
             if(i==nElems)
                return false;					
             else
             {
                for(int j = i; j < nElems; j++)
                   a[j][j] = a[j+1][j+1];					
                nElems--;							
                return true;
             }
          }		// end delete
       
           public void display()					
          {
             for (int i = 0; i < a.length; i++)
             {
                for (int j = 0; j < a[i].length; j++)
                {   
                   System.out.print(a[i][j] + " ");
                }       
                System.out.println("");
             }
          
          }
       }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by JinFTW
    but the elements are not in the right coordinates. The only one that is placed right is 0,0. Everything else just shows up in 1,1 or 2,2, or 3,3, or 4,4, instead of where they are supposed to go. Any hints would be greatly appreciated.
    That doesn't surprise me because the only elements you consider are a[x][x]
    for any value of x; (see your own code).

    kind regards,

    Jos

    Comment

    • JinFTW
      New Member
      • Feb 2008
      • 12

      #3
      Ah thank you for the advice, I reworked the insert code and I've come up with this, but now I can't get it to compile at all.

      Code:
      public void insert (int x, int y, int value)		// puts element into array
            {
               int i;														
               int j;
      			int a;
      			int b;
      			for (i = 0; i < nElems; i++)			// finds where it goes
          			for (j = 0; j < nElems; j++)        
      				if(a[i][j] > value)
                     break;
               for (a = nElems; a > i; a--)
      				for (b = nElems; b > j; b--)         		     
      				a[a][b] = a[a-1][b-1];			// moves bigger ones up
               a[i][j] = value;					// inser it
               nElems++;							// increment size

      Comment

      Working...