AngeL)
20-06-2007, 08:38
volevo creare una classe che emulasse uno stack, per capire come funzionano gli array, ma non ci sono riuscito :D
quando compilo ci sono molti errori, molti riferiti al metodo length():
questo è il codice:
public class FIFO {
int[] a;
public void add(int elem) {
int[] temp = a;
a = new int[a.length() + 1];
for (int i = 0; i < temp.length(); i++) {
a[i+1] = temp[i];
}
a[0] = elem;
}
public int pop() {
int[] temp = a;
a = new int[a.length() - 1];
for (int i = 0; i < temp.length() - 1; i++) {
a[i] = temp[i];
}
return temp[temp.length() - 1];
}
}
e questi sono gli errori:
C:\Users\AngeL\Desktop\FIFO.java:6: cannot find symbol
symbol : method length()
location: class int[]
a = new int[a.length() + 1];
^
C:\Users\AngeL\Desktop\FIFO.java:6: operator + cannot be applied to Array.length,int
a = new int[a.length() + 1];
^
C:\Users\AngeL\Desktop\FIFO.java:6: incompatible types
found : <nulltype>
required: int
a = new int[a.length() + 1];
^
C:\Users\AngeL\Desktop\FIFO.java:7: cannot find symbol
symbol : method length()
location: class int[]
for (int i = 0; i < temp.length(); i++) {
^
C:\Users\AngeL\Desktop\FIFO.java:15: cannot find symbol
symbol : method length()
location: class int[]
a = new int[a.length() - 1];
^
C:\Users\AngeL\Desktop\FIFO.java:16: cannot find symbol
symbol : method length()
location: class int[]
for (int i = 0; i < temp.length() - 1; i++) {
^
C:\Users\AngeL\Desktop\FIFO.java:19: cannot find symbol
symbol : method length()
location: class int[]
return temp[temp.length() - 1];
^
7 errors
Tool completed with exit code 1
che ho sbagliato? :confused:
quando compilo ci sono molti errori, molti riferiti al metodo length():
questo è il codice:
public class FIFO {
int[] a;
public void add(int elem) {
int[] temp = a;
a = new int[a.length() + 1];
for (int i = 0; i < temp.length(); i++) {
a[i+1] = temp[i];
}
a[0] = elem;
}
public int pop() {
int[] temp = a;
a = new int[a.length() - 1];
for (int i = 0; i < temp.length() - 1; i++) {
a[i] = temp[i];
}
return temp[temp.length() - 1];
}
}
e questi sono gli errori:
C:\Users\AngeL\Desktop\FIFO.java:6: cannot find symbol
symbol : method length()
location: class int[]
a = new int[a.length() + 1];
^
C:\Users\AngeL\Desktop\FIFO.java:6: operator + cannot be applied to Array.length,int
a = new int[a.length() + 1];
^
C:\Users\AngeL\Desktop\FIFO.java:6: incompatible types
found : <nulltype>
required: int
a = new int[a.length() + 1];
^
C:\Users\AngeL\Desktop\FIFO.java:7: cannot find symbol
symbol : method length()
location: class int[]
for (int i = 0; i < temp.length(); i++) {
^
C:\Users\AngeL\Desktop\FIFO.java:15: cannot find symbol
symbol : method length()
location: class int[]
a = new int[a.length() - 1];
^
C:\Users\AngeL\Desktop\FIFO.java:16: cannot find symbol
symbol : method length()
location: class int[]
for (int i = 0; i < temp.length() - 1; i++) {
^
C:\Users\AngeL\Desktop\FIFO.java:19: cannot find symbol
symbol : method length()
location: class int[]
return temp[temp.length() - 1];
^
7 errors
Tool completed with exit code 1
che ho sbagliato? :confused: