传递一个ArrayList的方法来打印尺寸 [英] Passing an ArrayList to method to print Size

查看:130
本文介绍了传递一个ArrayList的方法来打印尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个菜单系统,可以打印约一个ArrayList的各种信息。在我的第三个情况下,我不得不调用打印ArrayList的大小的方法。我试图调用的方法是相同的类的一部分。这些是我收到的错误消息。我将如何解决这些问题?

错误:

 非静态方法printrecordnumber(ArrayList的<车辆GT;)不能从静态上下文中引用
   案例3:的System.out.println(记录数:+ printrecordnumber(vehiclearray));

错误:

 无效类型此处不允许
   案例3:的System.out.println(记录数:+ printrecordnumber(vehiclearray));

下面是code我有:

  //菜单系统
做{
    的System.out.println(选项从排序的数据输入数字或停止停止程序\\ n1的。打印的所有数据\\ N2。打印所有数据(排序)\\ N3。记录打印数量\\ N4,打印自行车和卡车从区域code 987)\\ N5打印车辆;    尝试{
        字符串选项= input.next();        如果((option.equals(停止))){
            System.exit(0);
        }        INT optionint =的Integer.parseInt(选件);        开关(opt​​ionint){
            情况1:
                打破;
            案例2:
                打破;
            案例3:
                的System.out.println(记录数:+ printrecordnumber(vehiclearray));
                打破;
            情况4:
                打破;
            情况5:
                打破;
       }
    }
    赶上(NumberFormatException异常五){}
}而(真);//主要方法结尾括号
}
公共无效printrecordnumber(ArrayList的<车辆GT; vehiclearray){
   的System.out.println(vehiclearray.size());
}


解决方案

  

错误:非静态方法printrecordnumber(ArrayList中)不能
  从静态上下文的情况下3引用的内容:System.out.println(数
  记载:+ printrecordnumber(vehiclearray));


您不能调用从静态方法非静态方法。 <一href=\"http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static\">Here's更多关于为什么你不能做到这一点。 printrecordnumber()应该是一个静态方法。像这样的:

 公共静态无效printrecordnumber(ArrayList的&LT;车辆GT; vehiclearray){
   的System.out.println(vehiclearray.size());
}


  

错误:'无效'类型此处不允许的情况下3:的System.out.println(数
  中记载:+ printrecordnumber(vehiclearray));


您不能追加一个空白为String。换句话说,printrecordnumber()应返回一个对象。您可以从printrecordnumber返回列表的大小

 公共静态INT printrecordnumber(ArrayList的&LT;车辆GT; vehiclearray){
   返回vehiclearray.size();
}

I have a menu system that will print various information about an ArrayList. In my third case I have to call a method that prints the size of the arraylist. The method that I am trying to call is part of the same class. These are the error messages that I am receiving. How would I resolve these?

error:

non-static method printrecordnumber(ArrayList<vehicle>) cannot be referenced from a static context
   case 3:  System.out.println("Number of records: " + printrecordnumber(vehiclearray));

error:

'void' type not allowed here
   case 3:  System.out.println("Number of records: " + printrecordnumber(vehiclearray));

Here is the code I have:

//Menu System
do{
    System.out.println("Enter number of option or stop to stop the program\n1. Print all data\n2. Print all data (Sorted)\n3. Print number of records\n4. Print Bicycles and Trucks from the sorted data\n5. Print vehicles from area code 987");

    try{
        String option = input.next();

        if((option.equals("stop"))) {
            System.exit(0);
        }

        int optionint = Integer.parseInt(option);

        switch (optionint){
            case 1: 
                break;
            case 2: 
                break;
            case 3:  
                System.out.println("Number of records: " + printrecordnumber(vehiclearray)); 
                break;
            case 4: 
                break;
            case 5: 
                break;
       }
    }
    catch (NumberFormatException e){}
}while(true);

//Main Method Ending Bracket
}


public void printrecordnumber(ArrayList<vehicle> vehiclearray){
   System.out.println(vehiclearray.size());
}

解决方案

error: non-static method printrecordnumber(ArrayList) cannot be referenced from a static context case 3: System.out.println("Number of records: " + printrecordnumber(vehiclearray));

You cannot call a non-static method from a static method. Here's more on why you can't do that. printrecordnumber() should be a static method. Like this:

public static void printrecordnumber(ArrayList<vehicle> vehiclearray){
   System.out.println(vehiclearray.size());
}

error: 'void' type not allowed here case 3: System.out.println("Number of records: " + printrecordnumber(vehiclearray));

you cannot append a void to a String. In other words, printrecordnumber() should return an Object. You can return the size of List from printrecordnumber

public static int printrecordnumber(ArrayList<vehicle> vehiclearray){
   return vehiclearray.size();
}

这篇关于传递一个ArrayList的方法来打印尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆