Advertisement
davegimo

VECTORJAVA

Jun 5th, 2019
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. import java.util.Vector;
  2.  
  3. public class Studente {
  4.  
  5.     public String nome;
  6.     public String cognome;
  7.     public int matricola;
  8.  
  9.     public Studente(String n, String c, int m) {
  10.         nome = n;
  11.         cognome = c;
  12.         matricola = m;
  13.         }
  14.  
  15.     public String toString() {
  16.         return nome + " " + cognome + " " + matricola;
  17.         }
  18.  
  19.  
  20.         /*
  21.  * Metodi importanti:
  22.  *
  23.  * size() per il ciclo for
  24.  * add(Element)  per aggiungere un elemento
  25.  * remove(index) rimuovere elemento in un indice specifico
  26.  * get(index)
  27.  *
  28.  *
  29.  * */
  30.  
  31.  
  32.  
  33.     public static void main(String[] args) {
  34.         System.out.println("-----");
  35.  
  36.         Studente s = new Studente("Dave","Gimo",1);
  37.         Studente f = new Studente("Franco","Rossi",100);
  38.         Studente m = new Studente("Mario","Bianchi",777);
  39.  
  40.         Vector<Studente> v = new Vector<Studente>();
  41.  
  42.         v.add(s);
  43.         v.add(f);
  44.         v.add(m);
  45.  
  46.  
  47.         for (int i = 0; i < v.size(); i++ ) {
  48.  
  49.             if (v.get(i).nome == "Mario") {
  50.                 v.remove(i);
  51.                 }
  52.  
  53.  
  54.             }
  55.  
  56.  
  57.         //Ciclo di stampa
  58.         for (int i = 0; i < v.size(); i++ ) {
  59.  
  60.             System.out.println(v.get(i).toString());
  61.  
  62.             // array[i]
  63.  
  64.             }
  65.  
  66.         System.out.println("-----");
  67.  
  68.         }
  69.  
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement