Java 每项使用字符串数组项

Java 每项使用字符串数组项,java,arrays,Java,Arrays,我需要使用一个字符串数组来列出classrom中使用的频率 例如: String names[] = new String [50]; String names[0], then names[1], names[2] ... names[50], inserted names[0],[1],[2] then stop... now again inserting names[3],[4] and going on... 然后我需要调用一个又一个位置来插入一个名称 例如: String

我需要使用一个字符串数组来列出classrom中使用的频率

例如:

String names[] = new String [50];
String names[0], then names[1], names[2] ...  names[50],
inserted  names[0],[1],[2] then stop... now again inserting names[3],[4] and going on...
然后我需要调用一个又一个位置来插入一个名称

例如:

String names[] = new String [50];
String names[0], then names[1], names[2] ...  names[50],
inserted  names[0],[1],[2] then stop... now again inserting names[3],[4] and going on...
每一个都有不同的名称,并使用作业窗格插入。showInputDialog

如果可能,我希望从停止的下一个位置继续在阵列中插入

例如:

String names[] = new String [50];
String names[0], then names[1], names[2] ...  names[50],
inserted  names[0],[1],[2] then stop... now again inserting names[3],[4] and going on...
这是我现在拥有的

// reserved to use in menu loop

int option1=0;
int option2=0;

// array that will keep all the classrom students names

String names[] = new String [50];

//in need to find a way to fill position after position of the array with names

names[0] = JOptionPane.showInputDialog("Please, insert student name"); 

// in need to find a way to call next position in awway

names[1] = JOptionPane.showInputDialog("Please, insert student name");
您可能需要一个for(String current=new String():names){插入选项窗格}


这是你的作业还是什么

您可能需要一个循环。如果您希望能够保存状态并在以后继续,我将使用while循环和私有字段来保留当前索引,或者像前面建议的那样简单地使用ArrayList:

boolean keepGoing = true;
List<String> names = new ArrayList<String>();
while (keepGoing) {
    string newName = JOptionPane.showInputDialog("Please enter student name: ");
    if (newName == null) { //User has pressed "Cancel" or left the textbox empty
        keepGoing = false;
    } else {
        names.add(newName);
    }
}
boolean keepGoing=true;
列表名称=新的ArrayList();
同时(继续){
string newName=JOptionPane.showInputDialog(“请输入学生姓名:”);
如果(newName==null){//用户已按下“取消”或将文本框留空
keepGoing=false;
}否则{
名称。添加(新名称);
}
}
ArrayList允许您在不指定索引的情况下添加元素,从而避免了保存索引的麻烦。

由于这实际上是你的家庭作业,我将把它作为一个练习留给读者(一直想写这篇文章),让他们了解如何使用常规数组来完成它

使用计数器跟踪位置。。这将解决您的问题..如何使用字符串数组中的位置来接收JOptionPane.showInputDialog?我建议使用
ArrayList
而不是
String[]
。它有
add(…)
方法,可以在列表的末尾添加更多的项目。我现在开始编写代码,还没有完成任何工作…Petr你能给我展示一个使用此ArrayList的示例吗?是的,我是Java新手,这是一个家庭作业,所以,我需要把这一行插在哪里?stackoverflow不是你做作业的地方。您现在已经完成了哪些工作,我们将予以纠正或帮助您完成。你的代码片段是什么?我需要找到一种方法,用JOptionPane.showInputDialog在数组中输入一个位置。我将显示所做的。。。1分钟请在主要问题中添加代码,请看一看