Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为已存在的名称提供重写_Java_Arraylist_Treeset - Fatal编程技术网

Java 如何为已存在的名称提供重写

Java 如何为已存在的名称提供重写,java,arraylist,treeset,Java,Arraylist,Treeset,我有一个我想要的问题,当我在nameText type Text中输入一个库的名称,并且该名称已经存在于库的列表中时,按钮ok被阻止,我无法执行此操作,因为会显示消息并通知我将写入另一个名称 我不知道我到底使用了什么treeSet或arrayList以及如何使用?? 代码是: public static void removeDuplicates(List list) { Set set = new HashSet(); List newList = new Arra

我有一个我想要的问题,当我在nameText type Text中输入一个库的名称,并且该名称已经存在于库的列表中时,按钮ok被阻止,我无法执行此操作,因为会显示消息并通知我将写入另一个名称

我不知道我到底使用了什么treeSet或arrayList以及如何使用?? 代码是:

public static void removeDuplicates(List list)  
{  
    Set set = new HashSet();  
    List newList = new ArrayList();  
    for (Iterator iter = list.iterator(); iter.hasNext(); ) {  
        Object element = iter.next();  
        if (set.add(element))  
            newList.add(element);  
    }  
    list.clear();  
    list.addAll(newList);  
}  
在这方面:

 public class EditLibraryDialog extends Dialog implements ItemListener {

ILibrary library;
Text pathText;
Text nameText;
static boolean okPressed;

public EditLibraryDialog(Shell parent) {
    super(parent);
    library = null;
}

public EditLibraryDialog(Shell parent, ILibrary library) {
    super(parent);
    this.library = library;
}

public ILibrary getLibrary() {
    return library;
}

public boolean open() {
    return open(null);
}

public boolean open(String message) {

    final Shell parent = getParent();
    final Shell dialog = new Shell(parent, SWT.DIALOG_TRIM
            | SWT.APPLICATION_MODAL);

    okPressed = false;

    dialog.setSize(700, 150);
    dialog.setLayout(new GridLayout(4, false));
    GridData gridData = new GridData();
    dialog.setLayoutData(gridData);

    Label l;

    if (message != null) {
        l = new Label(dialog, SWT.NULL);
        l.setText(message);
    }

    l = new Label(dialog, SWT.NULL);

    l.setText("Name");

    nameText = new Text(dialog, SWT.BORDER);

    l = new Label(dialog, SWT.NULL);
    l = new Label(dialog, SWT.NULL);
    l = new Label(dialog, SWT.NULL);
    l.setText("Path");

    pathText = new Text(dialog, SWT.BORDER);

    });

    if (library == null) {
        dialog.setText("Add Library");
    } else {
        dialog.setText("Edit Library");
        nameText.setText(library.getName());
        pathText.setText(library.getPath());
    }

    final Button okButton = new Button(dialog, SWT.PUSH);
    okButton.setText("Ok");
    GridData gridData4 = new GridData(GridData.END, GridData.CENTER, false,
            false);
    gridData4.horizontalSpan = 3;
    gridData4.horizontalIndent = 20;
    okButton.setLayoutData(gridData4);
    okButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            String id;
            if (library != null) {
                id = library.getId();
            } else {
                id = UUID.randomUUID().toString();
            }
            library = new LibraryFactory().create(id, nameText.getText(),
                    pathText.getText());
            dialog.dispose();
            okPressed = true;



    }});




    nameText.addModifyListener(new ModifyListener() {
        String lastValidText = "";

        @Override
        public void modifyText(ModifyEvent e) {
            // avoid setting invalid values for the name text field
            String textValue = ((Text) e.getSource()).getText();
            if (parseName(textValue)) {
                lastValidText = textValue;
                okButton.setEnabled(textValue.equals("") ? false : true);
            } else {
                nameText.setText(lastValidText);
            }
        }

    });

    nameText.addVerifyListener(new VerifyListener() {
        @Override
        public void verifyText(VerifyEvent e) {
            Pattern pattern = Pattern.compile("[\\w| ]*");
            Matcher matcher = pattern.matcher(e.text);
            e.doit = matcher.matches();

        }
    });

    pathText.addVerifyListener(new VerifyListener() {
        @Override
        public void verifyText(VerifyEvent e) {
            Pattern pattern = Pattern.compile("[a-zA-Z0-9_.-\\\\\\/\\$]*");
            Matcher matcher = pattern.matcher(e.text);
            e.doit = matcher.matches();
        }
    });

    Button cancelButton = new Button(dialog, SWT.PUSH);
    cancelButton.setText("Cancel");

    cancelButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            dialog.dispose();
        }
    });


      public static void removeDuplicates(List list)  
      {  
        Set set = new HashSet();  
        List newList = new ArrayList();  
        for (Iterator iter = list.iterator(); iter.hasNext(); ) {  
          Object element = iter.next();  
          if (set.add(element))  
            newList.add(element);  
        }  
        list.clear();  
        list.addAll(newList);  
      }  




    if (nameText.getText().equals(""))
        okButton.setEnabled(false);
        JOptionPane.showMessageDialog(null,"Existed Library"); }


    dialog.open();
    Display display = parent.getDisplay();
    while (!dialog.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    return okPressed;
}

private boolean parseName(String newText) {
    return true;
}

@Override
public void itemStateChanged(ItemEvent e) {
    // TODO Auto-generated method stub

}
      }
使用ArrayList或Vector并使用contains方法进行检查


请删减到相关部分,以便重现您的问题。没有人愿意费力地阅读这么多的代码。我真的不知道你想要什么,或者问题出在哪里,但是重复的代码已经在publicstaticvoidremoveduplicateslist中删除了list@Theolodis方法public static void removeDuplicatesList list不运行,我尝试修改它,你能帮我如何添加或删除它吗work@Tichodroma我想添加一个方法,当我在nameText中添加一个新名称时,我可以验证该名称是否存在,如果它不存在,它将被添加到库列表中,否则我必须修改该名称有一个示例吗,因为我是Java新手,请帮助我,我想使用你的Idee,但我不知道如何使用
 Vector<String> v = new Vector<String>();
 v.add("apple");
 v.add("mango");

 if(v.contains("apple")) {
     //element already in list, do something
 }
 else {
     //element not in list, do something
 }