-1

提取形式主类:

Client k = getClient();
Truster kg = new Truster();
boolean ok = false;

while (!ok) {
    System.out.print("\n name: ");
    //io: is a class for my input from Keyboard

    kg.setName(io.readString()); // <--need the input from here

    //argu_vali is my ValidationClass
    if (argu_vali.analyse(k, "mylist") == 0) {
        ok = true;
    }
}

ok = false;
while (!ok) {
    System.out.print("\n: ");
    kg.setSumme(io.readInteger());
    if (argu_vali.analyse(kg, "sum") == 0) {
        ok = true;
    }
}

k.add_credit(kg);

我的班级客户:

class Client{
     String name;

     @t_list_Check
     private ArrayList <truster> t_list= new ArrayList<>();
}

我的班级委托人:

class Truster{
     String name;

     @Min(0)
     int sum;
}

我自己的 t_list ValidatorClass:

public class t_list_Validator implements ConstraintValidator<t_list_Check, Collection> {

    @Override
    public void initialize(t_list_Check a) {
    }

    @Override
    public boolean isValid(Collection kg_list, ConstraintValidatorContext cvc) {
        Object[] objects = kg_list.toArray();

        if (objects.length < 1) {
            return true;
        }

        String new_name = objects[objects.length-1].toString(); //<---need here the
                                                                //right input from above
        for (Object o : kg_list) {
            if (new_name.equals(o.toString())) {
                return false;
            }
        }
        return true;
    }
}

我现在的问题是,我只是为t_list. 对于我的信任者(kg.setName(io.readString());)的第二个输入,我从objects[objects.length-1].toString()ownValidator的最后一个元素中收到,t_list而不是实际元素。

例如:

  • 1.运行:
  • 主要输入:test1
  • t_list_Validator 中的值 new_name:test1

...

  • 2.运行:
  • 主要输入:测试2
  • value new_name in t_list_Validator: test1 // 此处需要实际输入:“test2”
4

1 回答 1

0

只需将元素添加到列表中即可解决问题。验证后将其从列表中删除,然后继续验证......

while (!ok) {
    System.out.print("\n name: ");


    kg.setName(io.readString()); 

    k.add(kg); // add here the element

    if (argu_vali.analyse(k, "mylist") == 0) {
        ok = true;

    }
    k.remove(k.getList().size()-1); //delete the element and go on with validation
}
于 2012-11-28T16:25:54.033 回答