Package coda;
import java.io.*;
import java.util.ArrayList;
class codap { public static void main (String[] args)
throws IOException{
InputStreamReader input=new InputStreamReader(System.in); BufferedReader h= new BufferedReader(input);
char ch;
ArrayList L=new ArrayList(0);
do{
System.out.println("A]push");
System.out.println("B]pop");
System.out.println("S]stampa"); System.out.println("X]EXIT");
ch=h.readLine().trim().charAt(0);
ch=Character.toUpperCase(ch);
switch(ch){
case 'A':push(L);break;
case 'B':L.remove(0);System.out.println(L);break;
case 'S':System.out.println(L);break;
default:if(ch!='X')
System.out.println("Opzione non valida") ; }
}while(ch!='X'); } //fine main
static void push(ArrayList T)throws IOException{
String s;
int i,n,k,j;
boolean trovato;
InputStreamReader input=new InputStreamReader(System.in); BufferedReader h= new BufferedReader(input);
do{
trovato=false;
System.out.print("elemento:");
s=h.readLine();
if(s.length()==0)break;
System.out.print("priorità:");
n=Integer.parseInt(h.readLine().trim());
Q q=new Q(s,n);
if(T.isEmpty()) T.add(q);
else{
for(i=0;i < T.size();i++){
k=((Q)T.get(i)).getPri();
if(n > k){T.add(i,q);trovato=true;break;}
}//fine for
if(trovato==false)T.add(q);
}//fine else
System.out.println(T);
}while(s.length()!=0);
}//fine push
} //fine classe codap
class Q {
private String ele;
private int pri;
public Q() {this("zero",1);}
public Q(String s,int j) {
if (j > 0 && j < 6 && s.length()!=0) {
ele=s;
pri = j;
} else {
System.out.println("Errore");
ele="zero";pri = 1; }
}
public String getEle() {return ele;}
public int getPri() {return pri;}
public String toString() {
return getEle()+":"+getPri();
}//fine toString
}//fine classe Q
Ho trovato questo programma che dovrebbe essere simile, non so se può aiutarti a capire meglio lamia richiesta