|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: May 2006
Città: Salerno
Messaggi: 936
|
[Java] FIFO artigianale
volevo creare una classe che emulasse uno stack, per capire come funzionano gli array, ma non ci sono riuscito
quando compilo ci sono molti errori, molti riferiti al metodo length(): questo è il codice: 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];
}
}
Codice:
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
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
temp.length
NON temp.length() e lo stesso in tutti gli altri punti Ma usare un IDE decente con il syntax highlighting, no eh?
__________________
|
|
|
|
|
|
#3 | |
|
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
Quote:
Poi comunque la tua tecnica di ridimensionare l'array e spostare fisicamente tutti gli elementi dell'array non è il massimo della efficienza .... In genere volendo usare un array, se si realizza un LIFO (stack) si gestisce la dimensione in modo "logico", non fisico. E nel caso di FIFO (coda) si usa array gestito in modo "circolare".
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) |
|
|
|
|
|
|
#4 | ||
|
Senior Member
Iscritto dal: May 2006
Città: Salerno
Messaggi: 936
|
Quote:
Quote:
in che modo?
|
||
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
Questa è una classe che avevo fatto un po' di tempo fa:
Codice:
class FixedQueue<E>
{
private Object[] elements;
private int front;
private int count;
public FixedQueue (int size)
{
if (size < 1)
throw new IllegalArgumentException ("size cannot be lower than 1");
elements = new Object[size];
front = 0;
count = 0;
}
public synchronized void insert (E item)
throws InterruptedException
{
while (count == elements.length)
wait ();
elements[(front+count) % elements.length] = item;
count++;
notifyAll ();
}
public synchronized E extract ()
throws InterruptedException
{
while (count == 0)
wait ();
@SuppressWarnings("unchecked")
E item = (E) elements[front];
front = (front + 1) % elements.length;
count--;
notifyAll ();
return item;
}
}
Ma a parte queste cose, il concetto che usa è appunto un array "circolare". Codice:
front
|
|
XXX------XXXXX
---->
-->
count
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: May 2006
Città: Salerno
Messaggi: 936
|
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 06:24.




















