【问题标题】:Error: Unable to access integer value which is size of list错误:无法访问列表大小的整数值
【发布时间】:2016-11-21 20:49:16
【问题描述】:

我正在尝试从我的类 NormalizeInput.java 访问 ThiArrayList.getAge().size()

这是我的包结构的样子:-ThisArrayList 和 Normalize Input 类都是公共类

我收到错误 create class temp,y as in my code ,

  public class NormalizeInput {

        List<Double> temp=new ArrayList<>();
        temp=ThisArrayList.getAge();
        int y=temp.size();

        for(int a;a<=y;a++) {

        }
    }

这是我的 getAge() 方法的样子:-

public static List<Double> getAge() {
    return ages;
}

我在哪里做错了,为什么我无法访问 int 值 y?

【问题讨论】:

  • 发布您的代码而不是结构以获得更好的帮助
  • getAge 返回什么?它的能见度如何?您应该发布一些代码以便我们能够帮助您。我们不能仅仅通过查看包结构来猜测
  • 你遇到了什么错误?
  • 你的静态变量ages从哪里来的ThisArrayList类?
  • 在这里我试图访问 y 的值并增加它,但我无法这样做,我的 netbeans 编辑器给出了在 normalizeneuralnetwork 中创建类 temp 并创建类 x 的建议,我很困惑为什么我无法访问这些值?

标签: java arraylist package


【解决方案1】:

我认为你的 getAge() 方法有问题..

测试类

class test{
    public static List<Double> getAge() {
        List<Double> ages = new ArrayList<>();
        //add some value here
        return ages;
    }
}

显示方式

public static void display(){
        test t = new test();
        List<Double> temp=new ArrayList<>();
        temp=t.getAge();
        int y=temp.size();
        System.out.println(y);
        for(int a=0; a<=y; a++) {
             //Your code
        }
    }

最小方法

public static void main(String arr[]){
     display();
}

这对我来说很好。

【讨论】:

  • 我从除 main 之外的类访问它,当我将它包含在函数 public void setNormalizeInput() { List&lt;Double&gt; temp=new ArrayList&lt;&gt;(); temp=ThisArrayList.getAge(); int y=temp.size(); for(int a=0;a&lt;=y;a++) { } },,,,,,, 中时,我的代码运行良好............ ....从来没有没有 main 方法........这是我缺少的 java 的某些功能吗?
  • 那么你想调用一个没有main方法的方法吗?
猜你喜欢
  • 2017-06-27
  • 1970-01-01
  • 2015-07-14
  • 2018-01-22
  • 2012-04-17
  • 2017-10-14
  • 1970-01-01
  • 2021-08-23
  • 1970-01-01
相关资源
最近更新 更多