Java中的getValue

时间:2015-12-16 04:55:57

标签: java arrays

我是Java的新手。获取基础知识,但基本代码仍然存在问题。无论如何我写了这段代码,但有一个错误,因为我不知道如何把它放在我的代码中。有人可以向我解释如何getValue或如何在我的代码中应用它?这可能很简单,但我很失落,以至于让这个工作。

    class ArrayPar
     {
      private long[] theArray;
      private int nElems;

    public ArrayPar(int max)
      {
       theArray = new long[max];
       nElems = 0;
      }

    public void insert(long value)
       { 
         theArray[nElems] = value;
         nElems++;
       }
    public int size()
      {
      return nElems;
      }

    public void display()

    {

    System.out.print("A=");

    for(int j=0; j<nElems; j++)

    System.out.print(theArray[j] + " ");

    System.out.println("");

    }

    public int partitionIt(int left, int right, long pivot)
           {
           int leftPtr = left - 1;       
           int rightPtr = right + 1;        
           while(true)
              {
              while(leftPtr < right &&     
                    theArray[++leftPtr] < pivot)
                 ;  // (nop)

              while(rightPtr > left &&      
                    theArray[--rightPtr] > pivot)
                 ;  // (nop)
              if(leftPtr >= rightPtr)       
                 break;                     
              else                          
                 swap(leftPtr, rightPtr);    
              }  // end while(true)
           return leftPtr;                   
           }  // end partitionIt()

    public int partitionIt2(int left, int right)
    {
       long pivot = theArray[nElems-1];
       int leftPtr = left - 1;
       int rightPtr = right + 1;

   while(true)
    {
     while(leftPtr < right &&     
     theArray[++leftPtr] < pivot)


              while(rightPtr > left &&      
                    theArray[--rightPtr] > pivot)
                 ;  // (nop)
              if(leftPtr >= rightPtr)       
                 break;                     
              else                          
                 swap(leftPtr, rightPtr);    
              }  // end while(true)
           return leftPtr;                   
           }  // end partitionIt()

    public void swap(int dex1, int dex2) 
    {

    long temp;
    temp = theArray[dex1];
    theArray[dex1] = theArray[dex2];
    theArray[dex2] = temp;
     }
       }

    class PartitionApp

    {

    public static void main(String[] args)
          {
          int maxSize = 10;              // array size
          ArrayPar arr;                 // reference to array
          arr = new ArrayPar(maxSize);  // create the array

          for(int j=0; j<maxSize; j++)  // fill array with
             {                          // random numbers
             long n = (int)(java.lang.Math.random()*99);
             arr.insert(n);
             }
          arr.display();                // display original array
                                        // partition the array
          int partition = arr.partitionIt2(0, maxSize-1);

          System.out.println("Partition is at index " + partition +
                          ", pivot=" + (arr.getValue(partition)) );
          arr.display();                // display partitioned array
          }  // end main()
       }  // end class PartitionApp

错误

(arr.getValue(partition)) );
    ^
  symbol:   method getValue(int)
  location: variable arr of type ArrayPar
1 error

1 个答案:

答案 0 :(得分:5)

您需要将getValue(int)添加到ArrayPar。这可能看起来像,

public long getValue(int x) {
    return theArray[x];
}