简体   繁体   English

在Java循环外传递变量

[英]Pass variable outside of loop, Java

I continue to get the error: variable V might not have been initialized. 我继续收到错误消息:变量V可能尚未初始化。 What am I doing wrong? 我究竟做错了什么? I need to continue to work with the variable V outside of the loop. 我需要继续在循环外部使用变量V。 Is it possible to so this? 这样有可能吗?

    String[] arr = _input.split(SEPARATOR);
    String[] V;
    int chunk = 1; // chunk size to divide
    for( int i=0;i<arr.length;i+=chunk){
        V = Arrays.copyOfRange(arr, i, Math.min(arr.length,i+chunk));
    }
    System.out.print(Arrays.toString(V));

只需将其初始分配为null

String[] V = null;

You must initialize the local variables before using it. 您必须先初始化局部变量,然后才能使用它。 Try this instead of String[] V; 试试这个代替String[] V;

String[] V = null;

It is not sure that you go into the loop. 不确定您是否进入循环。 In that case V really is not initialized. 在那种情况下, V实际上没有初始化。 Init the variable first String[] V = new String[0] . 初始化变量first String[] V = new String[0]

Array object V must be initialized first 数组对象V必须先初始化

try doing 尝试做

String[] V = null;

and give the array a more descriptive name... and follow oracle name conventions 并为数组指定一个更具描述性的名称...并遵循oracle名称约定

Set String[] v=null; 设置String[] v=null;

also why you are coping the array in a loop?? 还有为什么你要循环处理数组?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM