关于动态数组 "Unexpected type" 的问题
Question about "Unexpected type" for Dynamic Array
大家晚上好,打扰了
我在尝试自己配置“DynamicArray”时遇到问题。在这里我做了一个排序方法,但是终端显示“error:unexpected type”,我实际上很难解决这个问题,有人可以帮我检查一下我遇到了什么问题吗?
提前致谢。
这是我的代码:
package lab2;
public class DynamicArray {
private static int INITIAL_CAPACITY = 5;
private int[] data;
private int size;
public DynamicArray() {
data = new int[INITIAL_CAPACITY];
size = 0;
}
// Returns `true` if the array is empty.
public boolean isEmpty() {
if (size==0){
return true;
}
else{
return false;
}
}
// Returns the size of the array.
public int size() {
return size;
}
// Remove all elements from data.
public void clear() {
size=0;
}
// Create a `String` with the elements of the array separated by comma, without a new line character at the end.
// For instance: 4, 5, 6
public String toString() {
if(size==0){
return "";
}
StringBuilder a = new StringBuilder();
for(int i=0;i<size;i++){
if(i==size-1){
a.append(data[i]);
}
else{
a.append(data[i]).append(",").append(" ");
}
}
return a.toString();
}
// Returns `true` if the array `data` is full: no more element can be added to `data`.
// Returns `false` otherwise.
private boolean isFull() {
if(size==data.length){
return true;
}
return false;
}
// If the array `data` is full:
// 1. Create a new array `data2` doubling the size of data.
// 2. Copy the elements of `data` into `data2`.
// 3. Assign `data2` to `data`.
private void realloc() {
if(size==data.length){
int [] data2 = new int[data.length*2];
System.arraycopy(data, 0, data2, 0,data.length);
data=data2;
}
}
// The element `x` is added to `data`, and `size` is incremented by one.
// `data` is automatically resized if it is too small.
public void add(int x) {
realloc();
data[size++]=x;
}
private void checkIndex(int idx) {
if(idx >= size) {
throw new ArrayIndexOutOfBoundsException();
}
}
// Set the ith element of `data` to `x`.
public void set(int idx, int x) {
int a = data[idx];
data[idx]=x;
}
// Return the element at the index `idx` of `data`.
public int get(int idx) {
checkIndex(idx);
return data[idx];
}
// Remove the element at index `idx`.
// Shift all the elements after `idx` of one position to the left.
public void remove(int idx) {
checkIndex(idx);
for(int i = idx; i<size-1; i++){
data[i]=data[i+1];
}
size--;
}
}
以及出现问题的代码:
package lab2;
public class DynamicArrayTest {
public static void main(String[] args) {
DynamicArray array = new DynamicArray();
testAdd(array);
testRemove(array);
testGet(array);
testRealloc(array);
testSort(array);
}
public static void testAdd(DynamicArray array) {
System.out.println("Test add method.");
System.out.println(array.size());
System.out.println(array.isEmpty());
array.clear();
System.out.println(array.size());
System.out.println(array.isEmpty());
array.add(4);
array.add(5);
array.add(6);
System.out.println(array);
}
public static void testRemove(DynamicArray array) {
System.out.println("Test remove method.");
try {
for(int i = 0; i < 3; ++i) {
array.remove(1);
System.out.println(array);
System.out.println(array.size());
System.out.println(array.isEmpty());
}
}
catch(ArrayIndexOutOfBoundsException e) {}
array.remove(0);
System.out.println(array);
System.out.println(array.size());
System.out.println(array.isEmpty());
}
public static void testGet(DynamicArray array) {
System.out.println("Test get method.");
try {
array.get(4);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("OK");
}
}
public static void testRealloc(DynamicArray array) {
System.out.println("Test realloc method.");
for(int i = 0; i < 100; ++i) {
array.add(i);
}
System.out.println(array);
while(!array.isEmpty()) {
array.remove(0);
}
System.out.println(array);
System.out.println(array.size());
System.out.println(array.isEmpty());
}
public static void testSort(DynamicArray array){
System.out.println("Sort the array.");
for(int i =0; i<array.size(); ++i){
for (int j=0; j<array.size() - i; ++j){
if (array.get(j-1)<array.get(j)){
int a = array.get(j-1);
array.get(j-1)=array.get(j);
array.get(j)=a;
}
}
}
}
}
错误代码:
public static void testSort(DynamicArray array){
System.out.println("Sort the array.");
for(int i =0; i<array.size(); ++i){
for (int j=0; j<array.size() - i; ++j){
if (array.get(j-1)<array.get(j)){
int a = array.get(j-1);
array.get(j-1)=array.get(j);
array.get(j)=a;
}
}
}
}
再次,终端显示:
src/lab2/DynamicArrayTest.java:72: error: unexpected type
array.get(j-1)=array.get(j);
^
required: variable
found: value
src/lab2/DynamicArrayTest.java:73: error: unexpected type
array.get(j)=a;
^
required: variable
found: value
2 errors
非常感谢您的帮助。
array.get(j-1)=array.get(j);
array.get(j)=a;
应该是
array.set(j-1, array.get(j));
array.set(j, a);
此行试图为一个值赋值。该语句的左侧需要是一个变量。你在多个地方犯了同样的错误。
array.get(j-1)=array.get(j);
您要么需要将 array.get(j) 的值赋给一个变量,要么在数组对象上调用 setter,将 j-1 的位置设置为 array.get(j)。取决于你想用这个值做什么。
大家晚上好,打扰了
我在尝试自己配置“DynamicArray”时遇到问题。在这里我做了一个排序方法,但是终端显示“error:unexpected type”,我实际上很难解决这个问题,有人可以帮我检查一下我遇到了什么问题吗?
提前致谢。
这是我的代码:
package lab2;
public class DynamicArray {
private static int INITIAL_CAPACITY = 5;
private int[] data;
private int size;
public DynamicArray() {
data = new int[INITIAL_CAPACITY];
size = 0;
}
// Returns `true` if the array is empty.
public boolean isEmpty() {
if (size==0){
return true;
}
else{
return false;
}
}
// Returns the size of the array.
public int size() {
return size;
}
// Remove all elements from data.
public void clear() {
size=0;
}
// Create a `String` with the elements of the array separated by comma, without a new line character at the end.
// For instance: 4, 5, 6
public String toString() {
if(size==0){
return "";
}
StringBuilder a = new StringBuilder();
for(int i=0;i<size;i++){
if(i==size-1){
a.append(data[i]);
}
else{
a.append(data[i]).append(",").append(" ");
}
}
return a.toString();
}
// Returns `true` if the array `data` is full: no more element can be added to `data`.
// Returns `false` otherwise.
private boolean isFull() {
if(size==data.length){
return true;
}
return false;
}
// If the array `data` is full:
// 1. Create a new array `data2` doubling the size of data.
// 2. Copy the elements of `data` into `data2`.
// 3. Assign `data2` to `data`.
private void realloc() {
if(size==data.length){
int [] data2 = new int[data.length*2];
System.arraycopy(data, 0, data2, 0,data.length);
data=data2;
}
}
// The element `x` is added to `data`, and `size` is incremented by one.
// `data` is automatically resized if it is too small.
public void add(int x) {
realloc();
data[size++]=x;
}
private void checkIndex(int idx) {
if(idx >= size) {
throw new ArrayIndexOutOfBoundsException();
}
}
// Set the ith element of `data` to `x`.
public void set(int idx, int x) {
int a = data[idx];
data[idx]=x;
}
// Return the element at the index `idx` of `data`.
public int get(int idx) {
checkIndex(idx);
return data[idx];
}
// Remove the element at index `idx`.
// Shift all the elements after `idx` of one position to the left.
public void remove(int idx) {
checkIndex(idx);
for(int i = idx; i<size-1; i++){
data[i]=data[i+1];
}
size--;
}
}
以及出现问题的代码:
package lab2;
public class DynamicArrayTest {
public static void main(String[] args) {
DynamicArray array = new DynamicArray();
testAdd(array);
testRemove(array);
testGet(array);
testRealloc(array);
testSort(array);
}
public static void testAdd(DynamicArray array) {
System.out.println("Test add method.");
System.out.println(array.size());
System.out.println(array.isEmpty());
array.clear();
System.out.println(array.size());
System.out.println(array.isEmpty());
array.add(4);
array.add(5);
array.add(6);
System.out.println(array);
}
public static void testRemove(DynamicArray array) {
System.out.println("Test remove method.");
try {
for(int i = 0; i < 3; ++i) {
array.remove(1);
System.out.println(array);
System.out.println(array.size());
System.out.println(array.isEmpty());
}
}
catch(ArrayIndexOutOfBoundsException e) {}
array.remove(0);
System.out.println(array);
System.out.println(array.size());
System.out.println(array.isEmpty());
}
public static void testGet(DynamicArray array) {
System.out.println("Test get method.");
try {
array.get(4);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("OK");
}
}
public static void testRealloc(DynamicArray array) {
System.out.println("Test realloc method.");
for(int i = 0; i < 100; ++i) {
array.add(i);
}
System.out.println(array);
while(!array.isEmpty()) {
array.remove(0);
}
System.out.println(array);
System.out.println(array.size());
System.out.println(array.isEmpty());
}
public static void testSort(DynamicArray array){
System.out.println("Sort the array.");
for(int i =0; i<array.size(); ++i){
for (int j=0; j<array.size() - i; ++j){
if (array.get(j-1)<array.get(j)){
int a = array.get(j-1);
array.get(j-1)=array.get(j);
array.get(j)=a;
}
}
}
}
}
错误代码:
public static void testSort(DynamicArray array){
System.out.println("Sort the array.");
for(int i =0; i<array.size(); ++i){
for (int j=0; j<array.size() - i; ++j){
if (array.get(j-1)<array.get(j)){
int a = array.get(j-1);
array.get(j-1)=array.get(j);
array.get(j)=a;
}
}
}
}
再次,终端显示:
src/lab2/DynamicArrayTest.java:72: error: unexpected type
array.get(j-1)=array.get(j);
^
required: variable
found: value
src/lab2/DynamicArrayTest.java:73: error: unexpected type
array.get(j)=a;
^
required: variable
found: value
2 errors
非常感谢您的帮助。
array.get(j-1)=array.get(j);
array.get(j)=a;
应该是
array.set(j-1, array.get(j));
array.set(j, a);
此行试图为一个值赋值。该语句的左侧需要是一个变量。你在多个地方犯了同样的错误。
array.get(j-1)=array.get(j);
您要么需要将 array.get(j) 的值赋给一个变量,要么在数组对象上调用 setter,将 j-1 的位置设置为 array.get(j)。取决于你想用这个值做什么。