我有Fila
课程:
import java.util.Arrays;
public class Fila {
private int [] fila;
public Fila(int[] fila) {
this.fila = fila;
}
public Fila() {
}
public int[] getFila() {
return fila;
}
public void setFila(int[] fila) {
this.fila = fila;
}
@Override
public String toString() {
return "Fila{" +
"fila=" + Arrays.toString(fila) +
'}';
}
}
现在我想在main上创建一个新的int数组,并插入用户输入的新值。我的主要代码有这段代码:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner teclado = new Scanner(System.in);
System.out.println("Fila dimension? ");
int n = teclado.nextInt();
Fila [] a = new Fila [n];
System.out.println("Dimension of Fila: "+a.length);
System.out.println("Place that you want to add? ");
int l = teclado.nextInt();
for (int i = l-1; i <= l-1; i++) {
System.out.println("Number? ");
a [i] = teclado.nextInt();
}
System.out.println(Arrays.toString(a));
}
}
但我在这里有不兼容的类型:a [i] = teclado.nextInt();
我需要做什么?
答案 0 :(得分:0)
在我看来,你想要:
public class Main {
public static void main(String[] args) {
Scanner teclado = new Scanner(System.in);
System.out.println("Fila dimension? ");
int n = teclado.nextInt();
int [] a = new int [n];
System.out.println("Dimension of Fila: "+a.length);
System.out.println("Place that you want to add? ");
int l = teclado.nextInt();
for (int i = l-1; i <= l-1; i++) {
System.out.println("Number? ");
a [i] = teclado.nextInt();
}
System.out.println(Arrays.toString(a));
Fila fila = new Fila(a);
// Do something else with fila here...
}
}
当你写Fila a = new Fila [n]时,你实际上是在创建一个Fila对象数组,而不是一组整数。