Java Vector : Each element contains three objects. 如何根据其中之一的值进行操作?

标签 java vector element

例如,我有 vector (object1,object2,price)。如何打印价格 > 100 的元素?

所有教程和文档(关于以这种方式进行操作)我只见过处理每个元素仅包含一个对象的 vector 。

那么我怎样才能获得元素内一个特定对象的句柄呢?或者这有可能吗?

附带问题:这些叫什么?也就是说,如果单个元素由多个项目组成,那么这些项目叫什么?与数据库一样,记录由字段组成。很难用谷歌搜索你不知道名字的东西。

主要:

import java.util.Vector;

public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    String type;
    String location;
    double value;

    System.out.print("type->");
    type=sc.nextLine();

    System.out.print("location->");
    location=sc.nextLine();

    Property prop=new Property(type,location);

    System.out.print("value->");
    value=sc.nextDouble();

    InsuranceInfo insu=new InsuranceInfo(prop,value);
    container.addInsuranceInfo(insu);
}

InsInfoContainer 类:

public class InsInfoContainer {
    private Vector<InsuranceInfo> container;

    public InsInfoContainer() {
        container = new Vector<>(3, 1);
    }

    public void addInsuranceInfo(InsuranceInfo insu) {
        container.addElement(insu);
    }

public void print() {
        Iterator<InsuranceInfo> iter = container.iterator();
        while (iter.hasNext()) {System.out.println(iter.next());}
    }

保险信息类:

public class InsuranceInfo {
    public InsuranceInfo(Property prop, double value) {
        this.prop = prop;
        this.value = value;
    }

    private Property prop;
    private double value;

    public Property getProp() {return prop;}
    public void setProp(Property prop) {this.prop = prop;}
    public double getValue() {return value;}
    public void setValue(double value) {this.value= value;}
}

属性类别:

public class Property {

    private String type;
    private String location;

    public Property(final String type, final String location) {
        this.type = type;
        this.location = location;
    }

    public String getType() {return this.type;}
    public void setType(final String type) {this.type = type;}
    public String getLocation() {return this.location;}
    public void setLocation(final String sijainti) {this.location = location;}
}

最佳答案

您有一个容器来存储您的 InsuranceInfo :

private Vector<InsuranceInfo> container;

  1. 您的container称为 Collection
  2. 您的InsuranceInfo container 内的实例称为 element
  3. 你里面的“元素”InsuranceInfo ( Propertyvalue )称为 propertyfieldelement

迭代你的container集合,通常的方式是使用for循环或foreach循环:

public void print() {
    for (InsuranceInfo element: container) {
       if (element.getValue() > 100) { // Here is your condition to filter elements
          // Process your elements here
       }
    }
}

您还可以使用Iterator , Stream来做到这一点。

关于Java Vector : Each element contains three objects. 如何根据其中之一的值进行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50198354/

相关文章:

python - 如何获取选定的选项(Selenium 和 Python)

javascript - 如何优化字符串到 DOM 的转换?

java - 测量驻留在单独项目中的 Selenium 测试的代码覆盖率

java - 如何将字段名称应用于 ArrayList,然后向该 ArrayList 添加内容

java - Java中的方形显示

java - 从内存中移除 ArrayList 对象

c++ - 调整 vector 大小 C++

c++ - 跨 VS 项目的 STL vector 损坏

C++ 如何打印复合 vector 的内容

php - 检查 PHP 数组元素中是否设置了值