例如,
像这样显示这些变量是否会更节省内存:
std::cout << "First char is " << char1 << " and second char is " << char2;
Run Code Online (Sandbox Code Playgroud)
而不是这个:
std::cout << "First char is " << char1;
std::cout << " and second char is " << char2;
Run Code Online (Sandbox Code Playgroud)
当然,我并不是真的担心两行代码..但我正在努力学习更有效地编写代码
谢谢
我在使用Java为我的2D数组赋值时遇到了麻烦.代码的最后一行theGrid[rowLoop][colLoop] = 'x';是抛出ArrayIndexOutOfBoundsException错误.有人可以解释为什么会这样吗?
这是我的代码......
public class Main {
public static char[][] theGrid;
public static void main(String[] args) {
createAndFillGrid(10,10);
}
public static void createAndFillGrid(int rows, int cols) {
theGrid = new char[rows][cols];
int rowLoop = 0;
for (rowLoop = 0; rowLoop <= theGrid.length; rowLoop++) {
int colLoop = 0;
for (colLoop = 0; colLoop <= theGrid[0].length; colLoop++) {
theGrid[rowLoop][colLoop] = 'x';
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我确定有一个简单的解释,但我不能在其他函数下面调用函数.
int methodOne() {
std::cout << "Method 1";
methodTwo(); //Problem is here. I cannot call methodTwo()
return 0;
}
int methodTwo() {
std::cout << "Method 2";
return 0;
}
int main() {
methodOne();
}
Run Code Online (Sandbox Code Playgroud)