Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回所有输入的Java数组_Java_Arrays - Fatal编程技术网

返回所有输入的Java数组

返回所有输入的Java数组,java,arrays,Java,Arrays,在我的编程任务中,我需要安排几个人,对于每个人,除了第一个人,我需要添加联系人(每个人都可以选择要选择的联系人数量和联系人)。我不能使用列表或地图 Person[] contacts=new Person[Optinal]; //Opitional is an constant 在for循环中,我执行以下操作: if (i != 0) { // We have more than one person add contacts System.out.println(&quo

在我的编程任务中,我需要安排几个人,对于每个人,除了第一个人,我需要添加联系人(每个人都可以选择要选择的联系人数量和联系人)。我不能使用列表或地图

Person[] contacts=new Person[Optinal];
//Opitional is an constant
在for循环中,我执行以下操作:

if (i != 0) {
      // We have more than one person add contacts
    System.out.println("Enter number of contacts: ");
    int noContacts = scanner.nextInt();
    scanner.nextLine();

    if (noContacts < Optinal) {
        for (int k = 0; k < noContacts; k++) {
            contacts[k] = chooseContact(scanner, people, noContancts);
        }
    } else {
        System.out.println("Error! ");
    }
}
如果(i!=0){
//我们有多个人添加联系人
System.out.println(“输入联系人数量:”);
int noContacts=scanner.nextInt();
scanner.nextLine();
如果(无触点<可选){
for(int k=0;k
然后在方法中,我选择我想要的联系人:

 private static Person chooseContact(Scanner scanner, Person[] people, int noContacts){
    for (int j = 0; j < i; j++) {
        System.out.printf("%d. %s %s \n", j+1, people[j].getName(), people[j].getLastName());
    }

    int number = 0;
    do {
        System.out.print("Choose: ");

        while (!scanner.hasNextInt()) {
            System.out.println("Enter a number!");
            scanner.next();
        }

        broj=scanner.nextInt();
        scanner.nextLine();

        if (nunber < 0 || number > people.length) {
            System.out.println("Error, enter again");
        }
    } while (number < 0 || number > people.length);
    return people[number-1];
}
private static Person chooseContact(扫描器扫描器,Person[]人,int nocontact){
对于(int j=0;jpeople.length){
System.out.println(“错误,再次输入”);
}
}而(人数<0 | |人数>people.length);
返回人[1号];
}
我输入第一人称姓名:John姓氏:未知

它跳过第一人称,因此John没有联系人

第二个名字:玛丽姓:AlsoUknown

选择要选择的联系人数量->1
for循环应显示:

  • 约翰不知名
    第三人姓名:Johnny Last:Depp
    您要选择的联系人数:
    2
    对于循环显示:
    1.约翰不知名
  • 玛丽·艾尔索恩(Marry AlsoUnknown)
    1->我选择第一个人作为联系人放入数组
    2->我选择第一个人作为联系人放入阵列
  • 问题是,即使我有几个人可以选择,我也只能选择一个人作为联系人

    当我为
    person[I].getContacts().length执行for循环时,我总是得到3(作为联系人输入的总人数),而不是我为每个人选择作为联系人的人数


    如何在for循环中声明类型为
    Person[]contacts=new Person(noContacts)
    的对象并正确返回它?

    数组的长度是一个常量(您的选项)。数组中是否有空条目并不重要,长度总是相同的


    要么您需要逻辑来只计算非空值,要么您可以使用列表而不是数组。

    I added Person[]contacts=new Person[0];if(i!=0)语句之前和for循环中的contacts=newperson[noContacts]

    我不能使用列表。我需要在一个人中存储多个联系人。正如所声明的,我有3个人,并且我还放置了联系人数组。@Qyz93我假设这是家庭作业,否则“我不能使用列表存储多个联系人”毫无意义,这就是列表的用途。如果您必须使用数组,那么对于计数,您需要检查数组中有多少项是非空的。使用长度是不够的。