如何找到具有多个对象的arraylist的索引?

时间:2018-09-30 08:03:04

标签: java arraylist

您好,我想找到一种方法来使用一个或两个对象获取arraylist属性的索引。

财产等级: `

  public class Property implements Serializable{

   ArrayList <Task> tasks = new ArrayList<Task>();


String type; //Condo, HDB, Landed
String name;
int size; 
boolean rental;
String postcode;
String block;
String unit;


public Property(String type, String name, int size, boolean rental, String postcode, String block, String unit) {
    this.tasks = tasks;
    this.type = type;
    this.name = name;
    this.size = size;
    this.rental = rental;
    this.postcode = postcode;
    this.block = block;
    this.unit = unit;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}




public ArrayList<Task> getTasks() {
    return tasks;
}

public String getName() {
    return name;
}

public int getSize() {
    return size;
}

public boolean isRental() {
    return rental;
}

public String getPostcode() {
    return postcode;
}

public String getBlock() {
    return block;
}

public String getUnit() {
    return unit;
}

public boolean getRental(){
    return this.rental;
}

public void setName(String name) {
    this.name = name;
}

public void setSize(int size) {
    this.size = size;
}

public void setRental(boolean rental) {
    this.rental = rental;
}

public void setPostcode(String postcode) {
    this.postcode = postcode;
}

public void setBlock(String block) {
    this.block = block;
}

public void setUnit(String unit) {
    this.unit = unit;
}

进行大多数文件和内容设置的属性管理器类。

public class PropertyManager {



private static String fileName="Properties.ser";




public static ArrayList <Property> properties;

public static void main(String args[]){




  // check if file exists
    //load it into properties arraylist
  // if no create and empty properties arraylist

 FileInputStream fis;
 try{
fis = new FileInputStream(fileName);
 ObjectInputStream  ois;
     ois = new ObjectInputStream(fis);
     properties=(ArrayList <Property>)ois.readObject();
     fis.close();
      System.out.println("Loaded in a file with "+properties.size()+" properties");

 } catch (Exception e){
     System.out.println("There is no datafile - creating a new one");
     //e.printStackTrace();
     //properties=new ArrayList <Property>();
     testData();
 }


 MainScreen ms=new MainScreen();
  ms.setVisible(true);


}

public void AddProperty(String fName ){

}


public static void SavePropertiesToFile(){
         try{
 FileOutputStream outputFile = new FileOutputStream(fileName);
 ObjectOutputStream  oos;
     oos = new ObjectOutputStream(outputFile);
     oos.writeObject(properties);
     outputFile.close();
     System.out.println("Data file "+fileName+" has been saved");

 } catch (Exception e){
     System.out.println("There was an error saving the file:");
     e.printStackTrace();
     //properties=new ArrayList <Property>();
     testData();
 }

}
static void testData(){

        properties=new ArrayList<Property>();
        Random rand=new Random();
        Task t;
        Property p;

        for (int i=0;i<10;i++){
            //public Property(String name, int size, boolean rental, String postcode, String block, String unit) {
            p=new Property("CONDO","Property_"+i,20200,true,"280009","14","01-05");
            //public Task(char type, int priority, String description) {
            t=new Task('M',3,"This s a dummy task"+rand.nextInt());
            t=new Task('M',3,"This s a dummy task"+rand.nextInt());
            t=new Task('M',3,"This s a dummy task"+rand.nextInt());
            p.getTasks().add(t);
            properties.add(p);
        }
}

}

我想找到一种方法,可以在其中输入属性名称,然后单击“查找”,然后它将加载输入的整个属性。然后,我可以使用它来编辑或删除属性。

所以我想找到这样做的索引,但是如果有另一种方法而不是找到索引,我不会介意尝试一下。

我还看到了一些有关重写equals()或其他内容的信息,但我不知道如何实现。

1 个答案:

答案 0 :(得分:0)

使用适当的数据结构。如果要按名称查找属性,则建议使用app.controller("TestController", function (TestService, $timeout, $log) { let that = this; this.$onInit = function () { this.text = TestService.getCurrentText(); //debugging this.update(); }; //debugging this.update = function() { $timeout(function () { $log.debug(that.text); that.update(); }, 1000); } //debugging this.$onChanges = function (obj) { $log.debug(obj); } }); ,其中键是属性名称,值是属性本身。

java.util.Map中声明属性,如下所示:

PropertyManager

然后您的public static Map<String, Property> properties; 方法将如下所示:

testData()

现在,您可以轻松按名称查找属性:

static void testData(){

    properties=new HashMap<>();
    Random rand=new Random();
    Task t;
    Property p;

    for (int i=0;i<10;i++){
        String propertyName = "Property_"+i;
        //public Property(String name, int size, boolean rental, String postcode, String block, String unit) {
        p=new Property("CONDO",propertyName,20200,true,"280009","14","01-05");
        //public Task(char type, int priority, String description) {
        t=new Task('M',3,"This s a dummy task"+rand.nextInt());
        p.getTasks().add(t);

        properties.put(propertyName, p);
    }
}