Doubt object vector

1

I have a question about how to proceed, there are syntax errors as you can see. But my knowledge is basic and I'm trying to improve. I have the following program. A Register of Flights, only with number, origin, destination and number of places.

I created a Class

 package aeroporto;


public class aviao {
    aviao[] voo = new voo[i];

     int numero;
     String origem;
     String destino;
     int vagas;


 for(int i=0; i<5; i++)
void Cadastrar(int numero,String origem,String destino, int vagas)
{
    voo[i].numero = numero;
    voo[i].origem = origem;
    voo[i].destino = destino;
    voo[i].vagas = vagas;

}
}

and the main program is this

package aeroporto;
import java.util.Scanner;
public class Aeroporto {

public static void main(String[] args) {
  Scanner teclado = new Scanner(System.in);

  int numero,vagas,i=0;

  String origem,destino;


  int op;
  do{
      System.out.println("****Menu Aeroporto****");
      System.out.println("1. Cadastro de vôo");
      System.out.println("2. Consultar vôo");
      System.out.println("3. Reservar Passagem");
      System.out.println("5. Sair");
      //ler entrada
     op =teclado.nextInt();
     if(op==4)
     {
         System.exit(1);
     }
     switch(op){ 
        case 1:

            for(i=0 ; i<5 ; i++){

                System.out.print ("Número do voo: ");
                numero =teclado.nextInt();
                System.out.print("Origem : ");
                origem = teclado.next();
                System.out.print("Destino : ");
                destino = teclado.next();
                System.out.print("Número de vagas : ");
                vagas = teclado.nextInt();

                aviao.Cadastrar (numero, origem, destino,vagas);
            }

            break;
        }      

   }while(true);
}

}

I would ask the gentlemen and gentlemen available to point out my mistakes and how to proceed. Note: I'm trying to make this object vector instead of using Arraylist, because I have no idea how it does.

    
asked by anonymous 02.12.2017 / 17:03

1 answer

0

To use an arraylist, do so.

Collection collection = new ArrayList ();

After this you have created your ArrayList. To add items simply paste the name of your .add collection and within () the object to be added. example: collection.add (object1);

How ArrayList could help you with your problem. So, first I advise you to create a beans1 class, it adds its variables as well.

public class Beans1 {
    int numero,vagas;
    String origem,destino;
    Collection colecao = new ArrayList();
}

On top of the variables, right-click the NetBeans screen and select the "Insert Code" option, select Getter and Setter, check all the options and you're done.

Inside the first class where your Get's and Set's will be, create a void method to return the values.

Now in your main class create an Array that returns the objects of class Beans1.

    ArrayList <Beans1> obj = new ArrayList<Beans1>();
    Beans1 o = new Beans1();

    o.getNumero();
    o.getVagas();
    o.getOrigem();
    o.getDestino();
    obj.add(o);
    
09.12.2017 / 22:50