Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.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_Class_Operators - Fatal编程技术网

Java 为什么可以';我不能用我在前面一节课上做的数组来打印电话簿的号码吗?

Java 为什么可以';我不能用我在前面一节课上做的数组来打印电话簿的号码吗?,java,arrays,class,operators,Java,Arrays,Class,Operators,我正在为一个学校项目做一个电话簿阅读器。程序要求输入名字和姓氏。人们的姓名和电话号码存储在一个数组中。如果你没有输入名字,而姓氏被分配给了多个人,那么程序应该吐出多个人的电话号码。对不起,可能有点长 public static void main(String[] args) { //Where the user input starts, gotta get dat number Scanner scan = new Scanner(System.in); Syst

我正在为一个学校项目做一个电话簿阅读器。程序要求输入名字和姓氏。人们的姓名和电话号码存储在一个数组中。如果你没有输入名字,而姓氏被分配给了多个人,那么程序应该吐出多个人的电话号码。对不起,可能有点长

public static void main(String[] args) {

    //Where the user input starts, gotta get dat number
    Scanner scan = new Scanner(System.in);

    System.out.print("First Name? ");
    String first = scan.nextLine();

    System.out.print("Last Name? ");
    String last = scan.nextLine();

    String fullname = first + last;

    int[] count = new int[0];
    int counter=0;

    if(first.equals("quit") || last.equals("quit"))
        System.out.println("Bye");
    else
    {
        for(int l=0; l < 5; l++)
        {
            PhoneBook pb = new PhoneBook();
            PhoneEntry entry = pb.search(fullname);
            if( entry != null)
            {
                count[counter] = l; //this is where the issue lies
                counter++;
            }
        }

    }

    if(count.length != 0)
    {
        for(int x=0; x<count.length; x++ )
        {
            PhoneBook pb = new PhoneBook();
            System.out.println("Name: " + pb.phoneBook[count[x]].name + " Number: " + pb.phoneBook[count[x]].phone);
            x++;
        }
    }
}

class PhoneEntry
{
    String name;    // name of a person
    String phone;   // their phone number
    PhoneEntry( String n, String p )
    {
        name = n; phone = p;
    }
}

class PhoneBook
{ 
    PhoneEntry[] phoneBook; 
    PhoneBook()    // constructor
    {
        phoneBook = new PhoneEntry[ 5 ] ;
        phoneBook[0] = new PhoneEntry( "James Barclay", "(418)665-1223" ); 
        phoneBook[1] = new PhoneEntry( "Grace Dunbar",  "(860)399-3044" );
        phoneBook[2] = new PhoneEntry( "Paul Kratides", "(815)439-9271" );
        phoneBook[3] = new PhoneEntry( "Violet Smith",  "(312)223-1937" );
        phoneBook[4] = new PhoneEntry( "John Smith",     "(913)883-2874" );
    }

    PhoneEntry search( String targetName )  
    {
        for (int j=0; j<phoneBook.length; j++)
        {
            if ( phoneBook[ j ].

                    //stuff is to make it so that it only tests if its to upper case and if it contains the target
                    name.toUpperCase().contains( targetName.toUpperCase()))
                return phoneBook[ j ];
        }
        return null;
    }
}
publicstaticvoidmain(字符串[]args){
//在用户输入开始的地方,必须获取dat编号
扫描仪扫描=新扫描仪(System.in);
系统输出打印(“名字?”);
String first=scan.nextLine();
系统输出打印(“姓氏?”);
字符串last=scan.nextLine();
字符串fullname=first+last;
int[]计数=新的int[0];
int计数器=0;
if(first.equals(“quit”)| last.equals(“quit”))
System.out.println(“再见”);
其他的
{
对于(int l=0;l<5;l++)
{
电话簿pb=新电话簿();
PhoneEntry=pb.search(全名);
if(条目!=null)
{
count[counter]=l;//这就是问题所在
计数器++;
}
}
}
如果(count.length!=0)
{
对于(intx=0;x您写道:

 count[counter] = l; //this is where the issue lies
实际上,真正的问题在于:

 int[] count = new int[0];
您已经创建了大小为零的数组;即,您不能将任何元素放入到中。如果您尝试这样做,您将得到一个异常。可能这就是正在发生的情况


在Java中,数组的大小是固定的。你用给定的大小创建它们,它们将永远保持该大小。

我如何使它变得更大,因为我添加了越来越多的值?谢谢你的回答!你不能。唯一的“使数组更大”的方法是创建一个新数组。如果你需要一个“类似数组的”如果数据结构的大小可以更改,则最好使用
列表
而不是数组。