Java矢量容量找到矢量大小 [英] Java vector capacity finding the vector size

查看:132
本文介绍了Java矢量容量找到矢量大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取向量的内容。
我有一个存储用户详细信息的类,如名称和密码:

I am trying to get the contents of a vector. I have a class which stores user details such as name and password:

package Login;

/**
 *
 * @author Kutoma
 */
public class User{
    private String name;
    private String password;


    public User(String aName, String password){
     name = aName;
     this.password = password;

    }


    public boolean checkPassword(String pass)
    {
        if(password.equals(pass))
            return true;
             else
            return false;

    }
    public String getName(){
        return name;
    }
    public String getPassword(){
        return password;
    }

}

然后我有一个名为setOfUsers的类它会添加用户并找到他们的名字:

I then have a class called setOfUsers which adds users and finds their names:

package Login;


public class SetOfUsers extends Vector<User> {
private static SetOfUsers register =  null;

public SetOfUsers(){
    super();
 User temp = new User("Adam","12345");
 add(temp);
}
public static SetOfUsers getInstance(){
    if (register == null)           
        { register = new SetOfUsers(); }
    return register;    
}


public void addUser(User aUser){
    super.add(aUser);
}

public User findUserByName(String name)
{
    for(int i = 0; i < capacity(); i++){

      User user = elementAt(i); 
      if(user.getName().equals(name)){
          return user;
      }

    }
    return null;
}

目前我收到此错误java.lang.ArrayIndexOutOfBoundsException:1> = 1
但是,我已经使用容量来查找向量的大小,不知道我在哪里出错了。
我的主程序在点击登录按钮时有以下代码,它应打印出
用户详细信息

At the moment I am getting this error java.lang.ArrayIndexOutOfBoundsException: 1 >= 1 However, I have used capacity to find the size of the vector, not sure where I am going wrong here. My Main program has the following code when login button is clicked it should print out the user details

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:

    SetOfUsers register = SetOfUsers.getInstance();
    String name = nameText.getText();
    User user = register.findUserByName(name);
    String passTxt = passText.getText();
    user = register.findUserByName(passTxt);
    System.out.print(user);


}                         


推荐答案

您正在使用 capacity(),这与 size()不同。请参阅此SO答案,了解两者之间的差异: https://stackoverflow.com/a/2787405/1370556

You're using capacity() which is not the same as size(). See this SO answer for a nice introduction to the differences between the two: https://stackoverflow.com/a/2787405/1370556.

简而言之,您需要循环遍历 size()

In short, you'll want to loop through size() instead:

for(int i = 0; i < size(); i++){

顺便说一句,您可能希望切换到ArrayLists。请务必阅读Java中 Vector ArrayList 之间的差异:为什么Java Vector类被认为已过时或已弃用?

As an aside, you may want to switch to ArrayLists. Make sure to read up on the differences between Vector and ArrayList in Java: Why is Java Vector class considered obsolete or deprecated?.

这篇关于Java矢量容量找到矢量大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆