简体   繁体   English

java中的数组问题

[英]Array trouble in java

Hello I'm making a program that tracks the number of users that are logged on by entering names through textfield0. 您好我正在创建一个程序,通过textfield0输入名称来跟踪登录的用户数。 I hit a road block being new to java I still don't understand some things I was wondering how one would add names to the array User in real time and have the array be count it show it in textfield1. 我遇到了一个新的java路障我仍然不明白一些事情我想知道如何将数字实时添加到数组用户并让数组计算它在textfield1中显示它。

Thank you in advance. 先感谢您。

public class UserTracking {


    public static void main(String[] args){
        final Kong f = new Kong();
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(200,110);
        f.setVisible(true);

    }


}
class Kong extends JFrame implements  ActionListener
{
    JTextField textfield0 = new JTextField();
    JTextField textfield1 = new JTextField();
    JLabel  label0 = new JLabel("User");
    JLabel  label1 = new JLabel("Number of Users:");
    JButton btnon = new JButton("Log on");
    JButton btnoff = new JButton("Log off");
    String User[] ={};


    public Kong()
    {
        super("Shazam");

    JPanel panel1 = new JPanel(new BorderLayout());
        panel1.add(textfield0,BorderLayout.CENTER);
        panel1.add(label0,BorderLayout.WEST);
    JPanel panel2 = new JPanel(new BorderLayout(10,10));
        panel2.add(btnon,BorderLayout.WEST);
        panel2.add(btnoff,BorderLayout.EAST);
    JPanel panel3 = new JPanel(new BorderLayout());
        panel3.add(textfield1,BorderLayout.CENTER);
        panel3.add(label1,BorderLayout.WEST);



    JPanel frame =  new JPanel(new BorderLayout(5,5));
        frame.add(panel1,BorderLayout.NORTH);
        frame.add(panel2,BorderLayout.EAST);
        frame.add(panel3,BorderLayout.SOUTH);
        setContentPane(frame);
    }

    public void actionPerformed(ActionEvent e)
    {

    }
}

If you've had experience with C++, think of Java arrays as C++ arrays in that once their size is defined, it stays that way. 如果您有使用C ++的经验,那么将Java数组视为C ++数组,一旦定义它们的大小,它就会保持这种状态。 If you wish to increase the capacity, you need to recreate the array and add the contents of the old array inside. 如果您希望增加容量,则需要重新创建数组并在其中添加旧数组的内容。 Essentially this is what vector did. 基本上这就是矢量所做的。

In Java if you want an expandable array, you essentially want a List object. 在Java中,如果需要可扩展数组,则基本上需要List对象。 I would strongly encourage you to use an ArrayList which is essentially the equivalent of vector in Java, allowing you to add as many objects as you want without worrying about its capacity. 我强烈建议您使用一个ArrayList,它基本上相当于Java中的vector,允许您根据需要添加任意数量的对象,而不必担心其容量。

If you ever need an array, you can convert it using the toArray() method (though in my experience, ArrayList does everything you'd require). 如果你需要一个数组,可以使用toArray()方法转换它(尽管根据我的经验,ArrayList可以完成你需要的所有工作)。

The size of an array in java can't be changed after the first initialization. 第一次初始化后,无法更改java中数组的大小。

If you want a dynamic sized data container, you can use any implementation of the List interface. 如果需要动态大小的数据容器,可以使用List接口的任何实现。 For example ArrayList : 例如ArrayList:

ArrayList<String> user = new ArrayList<String>();

And to add each user to the ArrayList : 并将每个用户添加到ArrayList:

user.add(username);

使用ArrayList

你需要的不是一个数组,而是一个更灵活的数据类型(能够“增长”大小),就像一些List实现一样:ArrayList实际上是由一个数组支持的,但是你不需要自己管理(重新)分配。

You are also missing linking buttons to ActionListener. 您还缺少将按钮链接到ActionListener。 Once that is done, you could use ArrayList to add users to the list as they login and remove users from list as they logoff. 完成后,您可以使用ArrayList在登录时将用户添加到列表中,并在用户注销时从列表中删除用户。

private ArrayList<String> loggedOnUsers = new ArrayList<String>();

and

void actionPerformed(event)
{
    if(it's logOn button action)
    {
        add user to loggedOnUsers list
    }
    else
    {
       remove user from loggedOnUsers list
    }
}

Instead of taking String array you can take ArrayList it would be easier. 而不是采用String数组,你可以采取ArrayList,这将更容易。 Something like this 像这样的东西

ArrayList<String> user = new ArrayList<String>();

On some action like button or text change event write this 在按钮或文本更改事件等某些操作上写下此内容

user.add(textField1.getText());

The actionPerformed method should actionPerformed方法应该

  • add a string into the User array (which should be named users ) 在User数组中添加一个字符串(应该命名为users
  • ask the array its new length 询问阵列的新长度
  • change the value in the count textfield ( textfield1 , which really has a bad name) 更改计数文本字段中的值( textfield1 ,其名称确实错误)

The problem is that Java arrays have a fixed length. 问题是Java数组具有固定长度。 They can't grow. 他们无法成长。 That'w why java.util.ArrayList exists : it's a class that behaves like a dynamic-length array. 这就是为什么java.util.ArrayList存在的原因:它是一个行为类似于动态长度数组的类。 Read its javadoc to know how to use it. 阅读其javadoc以了解如何使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM