Java中的getInteger

时间:2015-12-16 08:01:46

标签: java arrays

我是Java的新手。我得到了基础知识,但没有,但仍然有一些基本代码的麻烦。无论如何,我写了这段代码,但我有两个错误,因为我不知道如何把它放在我的代码中。有人可以向我解释如何getInteger或如何在我的代码中应用它?

我知道如何进行用户输入的唯一方法是使用Java中的Scanner。不是这种方式。这是教授要我使用的主要方法,因此我无法编辑主要内容。这可能很简单,但我很失落,似乎无法让它发挥作用。

class ArrayIns1
{
    private long[] theArray;
    private int nElems;
    public int numCounts;

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

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

    public void display() {
        System.out.print("A= ");
        for (int j = 0; j < nElems; j++)
            System.out.print(theArray[j] + " ");
        System.out.println("");
    }

    public void quickSort() {
        recQuickSort(0, nElems - 1);
    }

    public void recQuickSort(int left, int right) {
        int size = right - left + 1;

        if (size <= 3)
            manualSort(left, right);
        else {
            long median = medianOf3(left, right);
            int partition = partitionIt(left, right, median);

            recQuickSort(left, partition - 1);
            recQuickSort(partition + 1, right);
        }
    }

    public long callSelection(int ind) {
        return recSelect(0, nElems - 1, ind - 1);
    }

    public long recSelect(int left, int right, int index) {
        int size = right - left + 1;
        if (size <= 3) {
            manualSort(left, right);

            return theArray[index];
        }

        long median = medianOf3(left, right);
        int partition = partitionIt(left, right, median);

        if (partition == index)
            return theArray[index];
        else if (index < partition)
            return recSelect(left, partition - 1, index);
        else
            return recSelect(partition + 1, right, index);
    }

    public long medianOf3(int left, int right) {
        int center = (left + right) / 2;

        if (theArray[left] > theArray[center]) {
            swap(left, center);
        }

        if (theArray[left] > theArray[right]) {
            swap(left, right);
        }

        if (theArray[center] > theArray[right]) {
            swap(center, right);
        }

        swap(center, right - 1);

        return theArray[right - 1];
    }

    public void swap(int dex1, int dex2)
    {
        long temp = theArray[dex1];
        theArray[dex1] = theArray[dex2];
        theArray[dex2] = temp;

        numCounts += 3;
    }

    /*
     * This is the main partition function .
     */
    public int partitionIt(int left, int right, long pivot)
    {
        int leftPtr = left;
        int rightPtr = right - 1;

        while (true)
        {
            while (theArray[++leftPtr] < pivot)
                numCounts++;

            while (theArray[--rightPtr] > pivot)
                numCounts++;

            if (leftPtr >= rightPtr)
            {
                numCounts++;
                break;
            }
            else
                swap(leftPtr, rightPtr);
        }

        swap(leftPtr, right - 1);

        return leftPtr;
    }

    public void manualSort(int left, int right)
    {
        int size = right - left + 1;

        if (size <= 1)
            return;

        if (size == 2)
        {
            if (theArray[left] > theArray[right])
            {
                numCounts++;
                swap(left, right);
            }
            return;
        }
        else
        {
            if (theArray[left] > theArray[right - 1])
            {
                numCounts++;
                swap(left, right - 1);
            }

            if (theArray[left] > theArray[right])
            {
                numCounts++;
                swap(left, right);
            }

            if (theArray[right - 1] > theArray[right])
            {
                numCounts++;
                swap(right - 1, right);
            }
        }
    }
}

class pp74
{
    public static void main(String[] args) {
        int maxSize = 7; // array size
        int k = 0; // arbitrary index

        ArrayIns arr; // reference to array
        arr = new ArrayIns(maxSize); // create 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 array
        // get k from user
        System.out.print("Enter k (smallest is 1): ");
        k = getInteger();

        // get value of k-th elem
        long value = arr.recSelect(0, maxSize - 1, k - 1);
        // print value
        System.out.println("Value of " + k + "the element is " + value);
        arr.display();
    } // end main()
}

错误:

error: cannot find symbol
       k = getInteger();
            ^
    symbol:   method getInteger()
    location: class pp74
1 errors

2 个答案:

答案 0 :(得分:1)

您的代码评论显示您要从用户那里读取integer的{​​{1}}值, 如果是这样,那么使用下面的代码来实现相同的

k

并创建import java.util.Scanner; .. .. // get k from user System.out.print("Enter k (smallest is 1): "); Scanner in = new Scanner(System.in); int k = in.nextInt(); 而不是ArrayIns1

的对象

答案 1 :(得分:1)

1)未定义以下方法getInteger:

如果您有权修改/更新pp74类。只需在此类中添加此方法即可。

static int getInteger(){
    /* something to treat */
    return 0;
}

它有效,即使你有一个错误,但我告诉你如何管理这个:)

package csstudent.stackoverflow;

/**
 * Created by slim.soltani on 16/12/2015.
 */
public class Test {
public static void main(String[] args) {
    int maxSize = 7; // array size
    int k = 0; // arbitrary index

    ArrayIns arr; // reference to array
    arr = new ArrayIns(maxSize); // create 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 array
    // get k from user
    System.out.print("Enter k (smallest is 1): ");
    k = getInteger();

    // get value of k-th elem
    long value = arr.recSelect(0, maxSize - 1, k - 1);
    // print value
    System.out.println("Value of " + k + "the element is " + value);
    arr.display();
} // end main()

static int getInteger() {
    /* something to treat */
    return 0;
}
}


class ArrayIns {
public int numCounts;
private long[] theArray;
private int nElems;

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

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

public void display() {
    System.out.print("A= ");
    for (int j = 0; j < nElems; j++)
        System.out.print(theArray[j] + " ");
    System.out.println("");
}

public long recSelect(int left, int right, int index) {
    int size = right - left + 1;
    if (size <= 3) {
        manualSort(left, right);
        return theArray[index];
    }

    long median = medianOf3(left, right);
    int partition = partitionIt(left, right, median);

    if (partition == index)
        return theArray[index];
    else if (index < partition)
        return recSelect(left, partition - 1, index);
    else return recSelect(partition + 1, right, index);
}

public long medianOf3(int left, int right) {
    int center = (left + right) / 2;

    if (theArray[left] > theArray[center]) {
        swap(left, center);
    }

    if (theArray[left] > theArray[right]) {
        swap(left, right);
    }
    if (theArray[center] > theArray[right]) {
        swap(center, right);
    }
    swap(center, right - 1);
    return theArray[right - 1];
}


public void swap(int dex1, int dex2) {
    long temp = theArray[dex1];
    theArray[dex1] = theArray[dex2];
    theArray[dex2] = temp;
    numCounts += 3;
}

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

            numCounts++;
        while (theArray[--rightPtr] > pivot)
            numCounts++;
        if (leftPtr >= rightPtr) {
            numCounts++;
            break;
        } else swap(leftPtr, rightPtr);
    }
    swap(leftPtr, right - 1);
    return leftPtr;

}

public void manualSort(int left, int right) {
    int size = right - left + 1;
    if (size <= 1)
        return;
    if (size == 2) {
        if (theArray[left] > theArray[right]) {
            numCounts++;
            swap(left, right);
        }
        return;
    } else {
        if (theArray[left] > theArray[right - 1]) {
            numCounts++;
            swap(left, right - 1);
        }

        if (theArray[left] > theArray[right]) {
            numCounts++;
            swap(left, right);
        }

        if (theArray[right - 1] > theArray[right]) {
            numCounts++;
            swap(right - 1, right);
        }
    }
}
}