简体   繁体   English

使用Java,如何设置数组以存储下一个索引中的下一个用户输入/

[英]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.

相关问题 如何解决下一行的用户输入打印问题 - How do i fix the issue of user input printing on the next line 如何在Java中将用户输入添加到并行数组? - How do I add user input to parallel arrays in java? 如何针对两个数组验证Java中的用户输入? - How do I validate user input in java against two arrays? JAVA如何用txt文件中的数组替换scnr.next()以避免用户键入数千次? - JAVA how can I replace scnr.next() with the arrays in txt file to avoid user to type thousands of times? 用户输入的空格分隔值如何存储,直到它们在 java 的下一次迭代中分配给变量? - How do the user input space separated values get stored until they are assigned to the variable in the next iteration in java? JAVA:如何确定字符串中下一个字符的索引不等于一组定界符 - JAVA :How to determine the index of the next character in a string NOT equal to a set of delimiters 使用数组java更改下一次迭代的颜色 - Changing colors of next iteration using arrays java 如何使用keyboard.next()。charAt(0)将值存储到数组? 在java中 - how to store values to array using keyboard.next().charAt(0)? in java 如何在Java中使用Scanner类在一个String变量中存储两个或多个单词,然后在另一个String变量中存储下一个输入? - How do you use the Scanner class in java to store two words or more in a String variable, then the next input in another String variable? 如何使用java在for循环中获取当前和下一个arraylist索引 - How to get current and Next arraylist index in for loop using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM