java 数组作用域,java程序中if语句的作用域错误

这篇博客探讨了在Java编程中遇到的作用域问题,特别是关于在if语句内部声明的数组变量无法在外部使用的情况。作者指出,由于数组大小取决于变量N,因此必须在if/else语句内声明。解决方案是将数组声明移出if块,但在每个分支中初始化不同的大小。另一种简洁的解决方法是一行内根据条件直接初始化数组。这涉及到理解Java的变量作用域和条件运算符的知识。
摘要由CSDN通过智能技术生成

I'm having an issue with scope in an if statement, at least, I'm pretty sure that is where my error is, and I'm not sure of how to fix the issue (I'm pretty new at programming).

Basically, it seems that if I declare something within an if statement, the variable (in this case, an array of structs) does not exist outside of the if statement. However, I really need the declaration for the array to be inside of an if/else because the size of the array is dependent upon N, so how can I fix this error?

The program is in Java, and I'm using Eclipse. Any insight is greatly appreciated.

//declare an int (used in determining array length)

int N = 4;

//declare instance of MyClass

MyClass myClass = new MyClass();

//declare and array, then initialize each struct in that array

if(N <= 26){

MyStruct array[] = new MyStruct[260];

for(int i = 0; i < array.length; i++){

array[i] = new MyStruct();

}

}

else{

MyStruct array[] = new MyStruct[N*10];

for(int i = 0; i < array.length; i++){

array[i] = new MyStruct();

}

//lots of other code goes here of stuff that needs to be done before myMethod can be called

//call a method in myClass that requires 'array' to be passed in

myClass.myMethod(array); // ERROR - ARRAY CANNOT BE RESOLVED TO BE A VARIABLE

解决方案

You need to move the array declaration MyStruct array[]; outside of the if block. You answered your own question, in fact, when you declare a local variable inside a block (a piece of code surrounded by {}), the variable will only be visible inside that block, as per the scoping rules of the Java language.

What you can do inside the if or else blocks, is instantiating the array to the correct size, like this:

MyStruct[] array;

if (N <= 26) {

array = new MyStruct[260];

for (int i = 0; i < array.length; i++) {

array[i] = new MyStruct();

}

}

else {

array = new MyStruct[N*10];

for (int i = 0; i < array.length; i++) {

array[i] = new MyStruct();

}

}

An even shorter solution would be:

MyStruct[] array = new MyStruct[N <= 26 ? 260 : N*10];

for (int i = 0; i < array.length; i++) {

array[i] = new MyStruct();

}

RuleWizard for C++Test User’s Guide Table of Contents Introduction Welcome to RuleWizard! .....................................................................................................................1 Contacting Parasoft .............................................................................................................................4 The RuleWizard GUI Overview .............................................................................................................................................5 The Menu Bar .....................................................................................................................................7 Tool Bar ..............................................................................................................................................9 Tab Bar and Tab Panel ........................................................................................................................12 Messages Panel .................................................................................................................................16 Status Bar ...........................................................................................................................................17 New File Panel ....................................................................................................................................18 RuleWizard Preferences Panel ............................................................................................................22 RuleWizard Fundamentals What is a Rule? ..................................................................................................................................26 How to Create a Rule ..........................................................................................................................29 Automatic Rule Creation ......................................................................................................................36 How to Save a Rule .............................................................................................................................37 Enforcing Custom Coding Standards ..................................................................................................38 Working With Node Sets ......................................................................................................................40 Using Python Scripts in Rule Definitions ..............................................................................................52 RuleWizard Tutorial Tutorial: Introduction ............................................................................................................................57 Basic Rules Lesson 1: Do not use the ?: operator ..................................................................................................58 Lesson 2: Function Names Must Begin With a Capital Letter ..............................................................65 Lesson 3: Assignment Within an IF Statement ....................................................................................74 Lesson 4: Cast an Unsigned Character ...............................................................................................78 Lesson 5: Creating Rules With the Auto-Create Rules Feature ..........................................................85 C++Text Rules Lesson 6: No Whitespace Between Variables and Operands .............................................................95 Lesson 7: Single ASCII Space Between Conditional Statement and Its Opening Parenthesis ...........98 Advanced Rules Lesson 8: Return Rule .........................................................................................................................104 Lesson 9: Unused Variables ................................................................................................................113 Lesson 10: Using the OR Logical Component .....................................................................................119 Lesson 11: Using Containers ...............................................................................................................123 ii Lesson 12: Creating Format Component .............................................................................................131 Lesson 13: Using a Python Method to Enforce a File Naming Rule ....................................................136 Reference Guide RuleWizard Commands .......................................................................................................................141 Expressions and Regular Expressions ................................................................................................160 Available C/C++ Rule Nodes ...............................................................................................................163 Available C++Text Rule Nodes ............................................................................................................194 Index Index ...................................................................................................................................................221
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值