[英]Using Java, how do I set arrays to store the next user input in the next index/
What I'm trying to do is set up 14 arrays that will be of both type string and double to be able to accept input into the first index, and then the next time that the user enters in their information I don't want to add that information into an index that's already been given a value, but I do want to add that new information into the next index of that array. 我正在尝试做的是设置14个数组,它们都是字符串和double,以便能够接受输入到第一个索引,然后下次用户输入他们的信息我不想将该信息添加到已经赋值的索引中,但我确实想将该新信息添加到该数组的下一个索引中。 Do I just ++Array[index]?
我只是++ Array [index]吗? I'm also trying to set up this program to allow the user to be able to access 1 of 3 different IFstatements at a time to input values.
我也试图设置这个程序,让用户能够一次访问3个不同的IF语句中的1个来输入值。 If they enter input into an Array in IFstatement1, and then leave IFstatement1, go to IFstatement2 to input something, and then want to come back to IFstatement1 and input data into Arrays there, will the program know to add the newest user inputs into the next index in the array by using the ++Array[index] indicator, or do I have to do something else?
如果他们在IFstatement1中输入数组,然后离开IFstatement1,转到IFstatement2输入内容,然后想回到IFstatement1并将数据输入到Arrays中,程序是否知道将最新的用户输入添加到下一个使用++ Array [index]指标在数组中建立索引,还是必须做其他事情? How do I accomplish this?
我该如何做到这一点?
Try looking into the ArrayList object this should work for what you are looking for and it has a nice .add() function to add to the end of the list. 尝试查看ArrayList对象,这应该适用于您要查找的内容,并且它有一个很好的.add()函数可以添加到列表的末尾。
List<String> l1 = new ArrayList<String>();
l1.add("String");
l1.add("Another");
for (String str : l1) {
System.out.println(str);
}
This will add the strings and provide you the iterator. 这将添加字符串并为您提供迭代器。
If you want another with Doubles just change for or you can have one without generics that will just hold objects <> remove them. 如果你想要另一个双打改变,或者你可以有一个没有泛型只会持有对象<>删除它们。 Then your for loop would traverse the objects instead.
然后你的for循环将遍历对象。
You can't use ++Array[index]
to add an element to an array in Java. 您不能使用
++Array[index]
在Java中向数组添加元素。 As in other statically typed languages, array's are given a size when they are created and can't contain more values then their initial size. 与其他静态类型语言一样,数组在创建时会被赋予一个大小,并且不能包含更多的值,而不是它们的初始大小。 So for instance
int numbers[10];
所以例如
int numbers[10];
can hold 10 integer values. 可以容纳10个整数值。 If you want to set the values in that array use something like this:
如果要设置该数组中的值,请使用以下内容:
int numbers[10];
for (int i = 0; i < 10; i++) {
numbers[i] = 1 + 1;
}
With regards to the user input you describe, you would need to use some form of looping, maybe a while
loop? 关于您描述的用户输入,您需要使用某种形式的循环,也许是一个
while
循环?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.