Java >> Tutoriel Java >  >> Tag >> class

Classe vectorielle en Java

Vector est une collection qui contient des objets hétérogènes qui peuvent contenir des objets de différents types de classe, puis chaque objet est converti en type dans sa classe d'origine. Le vecteur est synchronisé, c'est-à-dire que plusieurs threads peuvent travailler simultanément sur un vecteur, ce qui ralentit le vecteur .

    Vector V = new Vector ();
    V.add(new Student('100','ramesh'));
    V.add(new Company('101','Airtel'));

Ici, Vecteur V contient deux objets de classes différentes Étudiant et Objet.

Nous pouvons également ajouter des valeurs de type entier, flottant ou chaîne dans le vecteur en tant qu'objet. Ces valeurs ne sont pas nécessaires pour le transtypage.

Avantages du vecteur

  1. Les vecteurs peuvent contenir des objets de différentes classes.
  2. Les vecteurs sont synchrones.

Inconvénient du vecteur

Le vecteur est lent

Comprenons le vecteur plus clairement en utilisant l'exemple ci-dessous :

import java.util.Vector;

// class representing the Student 
class Student {
  // data members of class	
  private int rollno;
  private String name;
  private String schoolCode;

  // Constructor
  public Student(int rollno, String name, String schoolcode) {
    this.rollno = rollno;
    this.name = name;
    this.schoolCode = schoolcode;
  }
  
  // putStudent() to print the values of the student Object
  public void putStudent() {
    System.out.println("RollNo :" + this.rollno + "\nName :" + this.rollno + "\nSchoolCode :" + this.schoolCode);
  }
}

// class representing the employee
class CompanyEmployee {
  // data memebers of the class
  public int id;
  public String name;
  public long salary;
  
  // constructor
  public CompanyEmployee(int id, String name, long salary) {
    this.id = id;
    this.name = name;
    this.salary = salary;
  }
  
  // putEmployee() to print the values of the employee object
  public void putEmployee() {
    System.out.println("Id :" + this.id + "\nName :" + this.name + "\nSalary :" + this.salary);
  }
}

// main class 
public class ExVector {
  public static void main(String[] args) {
    Vector V = new Vector(); //Vector class to hold the objects
    // adding CompanyEmployee Object to Vector 
    V.add(new CompanyEmployee(100, "Harendra Chekkur", 34000)); 
    // adding Student Object to Vector
    V.add(new Student(10, "Madhav Singh", "CB2025")); 
    // adding Integer as Object to Vector
    V.add(new Integer(70)); 
    // adding String as an Object to Vector
    V.add(new String("Testing Vectors")); 
    // adding employee as Object to Vector
    V.add(new Float(57.4)); 

    // iterating the vector to print the Objects 
    for (Object O: V) {
      /* as Vector holds hetrogeneous data objects,
      thus we have to cast the object to it's type
      in order to do this we are using getName() function 
      which gets the name of the class of the given object 
      and compares it with the given class , 
      if it's matches than typecast the object to that class */
      if (O.getClass().getName().equals("logicProgramming.CompanyEmployee")) {
        System.out.println();
        ((CompanyEmployee) O).putEmployee();
      } else if (O.getClass().getName().equals("logicProgramming.Student")) {
        System.out.println();
        ((Student) O).putStudent();
      }
      // if no match is found that is we will simply print th Object 
      else {
        System.out.println();
        System.out.println(O);
      }
    }
  }
}

Sortie

Id :100
Name :Harendra Chekkur
Salary :34000

RollNo :10
Name :10
SchoolCode :CB2025

70

Testing Vectors

57.4

Balise Java