gpt4 book ai didi

Java自动增长数组

转载 作者:行者123 更新时间:2023-11-29 02:59:04 24 4
gpt4 key购买 nike

<分区>

我正在尝试制作一个程序(只是为了好玩),它接受用户输入和密码,并将它们添加到数组中。但是由于我没有定义数量的用户和密码,我想通过执行以下操作使其自动增长:

static String[] passes = new String[i];

然后在某处 i++;

像这样:(inputUser 是一个由扫描仪赋予值的字符串)

private static void Update() {
i++;
user[i] = inputUser;
}

结果:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

我试过了,但没有用,也许我做错了什么?

请求代码:

package users.main;

import java.util.List;
import java.util.Scanner;

public class Main {

static int i = 0;
static List<String> users = new ArrayList<String>; // << Also highlighted red. Do i need ()?
// ^^^ Error
static String[] passes = new String[i];
static String inputUser = null;
static String inputPass = null;

public static void main (String[] args) {
@SuppressWarnings("resource") // <--- Eclipse wanted me to add this?....
Scanner sc = new Scanner(System.in);
String userInput = sc.nextLine();

switch (userInput) {
case "adduser":
AddUser();
break;

default:

System.out.println("Unknown command");
main(args);
}
}

UPDATE: I have changed the array to a list, but am still getting some errors.

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com