gpt4 book ai didi

java - 尝试调用方法时出现"invalid method declaration; return type required"

转载 作者:行者123 更新时间:2023-12-01 17:05:15 28 4
gpt4 key购买 nike

我是 Java 新手,在整理列表时遇到一些问题。我必须编写 Iterable 类,以便当您迭代队列时它会以随机顺序返回项目。这就是为什么我编写了 shuffle 方法,然后在 RandomizedQueueIterator 类中调用它

import java.util.Iterator;
import java.util.NoSuchElementException;
public class RandomizedQueue<Item> implements Iterable<Item>
{
// instance variables
private Item[] queue;
private int N = 0;
private int R = 0;


// constructor
public RandomizedQueue()
{
queue = (Item[]) new Object[1];
}


// helper functions
private void resize(int capacity)
{
Item[] copy = (Item[]) new Object[capacity];
for (int i = 0; i < N; i++)
copy[i] = queue[i];
queue = copy;
}

private void shrink(int capacity)
{
Item[] copy = (Item[]) new Object[capacity];
int M = 0;
for (int i = 0; i < N; i++)
{
if (queue[i] != null)
copy[M++] = queue[i];
}
queue = copy;
}

// shuffle queue
private void shuffle(Item[] a )
{
int N = a.length;
for (int i = 0; i < N; i++)
{
int r = StdRandom.uniform(i + 1);
Item temp = a[r];
a[r] = a[i];
a[i] = temp;
}
}


// return wether queue is empty
public boolean isEmpty() { return queue.length == 0; }


// return size of queue
public int size() { return queue.length; }


// add item to the queue
public void enqueue(Item item)
{
if (N == queue.length) { resize(N*2); }
queue[N++] = item;
}


// remove and return a random item from the queue
public Item deque()
{
// generate a random index and store item
int random_index;
do {
random_index = StdRandom.uniform(0, N);
} while (queue[random_index] == null);

Item item = queue[random_index];

// delete item from array and increment counter of null items
queue[random_index] = null;
R += 1;

// shrink array if corresponds
if (R == (3/4 * N)) { shrink(N/2); }

// return item
return item;
}

// return a random item from the queue
public Item sample()
{
// generate a random index and store item
int random_index;
do {
random_index = StdRandom.uniform(0, N);
} while (queue[random_index] == null);

Item item = queue[random_index];

// return item
return item;
}


// iteration
public Iterator<Item> iterator()
{
return new RandomizedQueueIterator();
}

private class RandomizedQueueIterator implements Iterator<Item>
{
private int i = 0;
shuffle(queue);


public boolean hasNext()
{
return i < N;
}

public Item next()
{
if (i >= N)
{
throw java.util.NoSuchElementException;
}
return queue[i++];

}

public void remove()
{
throw new UnsupportedOperationException("Invalid operation for array");
}
}


// main method
public static void main(String[] args)
{
RandomizedQueue example = new RandomizedQueue();
example.enqueue(0);
example.enqueue(1);
example.enqueue(2);
example.enqueue(3);
example.enqueue(3);
example.enqueue(3);
example.enqueue(3);
}
}

尝试编译时的输出:

2 errors found:

[line: 114] Error: invalid method declaration; return type required

[line: 114] Error: expected

line:114 > shuffle(queue);

任何帮助将不胜感激。谢谢

最佳答案

如果你想运行该类,你需要将方法调用放在主方法中

public static void main(String...args) {
shuffle(list_test);
}

如果您这样做,您可以将 list_test 定义为 static 变量,或者将其定义在 main 方法中。

关于java - 尝试调用方法时出现"invalid method declaration; return type required",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25880045/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com