關於三維陣列變數重置為0的注意事項

2021-07-05 11:59:39 字數 791 閱讀 7993

今天下午在寫**時,用到了三維陣列。i是自增的,當i=0時,一切正常,當i=1時,發現i=0陣列下所有的值都為0,鬱悶了2個小時,終於發現了問題。

因為每次我用for迴圈,都會new乙個新的三維陣列,i增加時,原來的三維陣列被new了一下,原來的值就消失了

錯誤**如何:

public int getquarter2(string year){

string starttime = year + "-4-1";

string endtime = year + "-6-30";

int orderid = this.getorderid(starttime,endtime);

int temp = new int[0][0][0]; //不同訂單的商品資訊陣列

for(int i=0; i

public int getquarter3(string year){

string starttime = year + "-7-1";

string endtime = year + "-9-30";

int orderid = this.getorderid(starttime,endtime);

int temp = new int[0][0][0]; //不同訂單的商品資訊陣列

for(int i=0; i

加了個if判斷就輕鬆解決了,不斷宣告新陣列的問題。避免了不斷new,丟掉原來陣列的麻煩。

另外不免感慨一下,一行**可以解決的問題,發現問題需要2個小時。

關於三維陣列和指標的測試

include int main void 用下面的表示看起來比較直接對得上 int a 2 3 4 下乙個就是0xbfd3d270 for i 0 i 2 i printf na p n a a表示陣列a 2 首元素a 0 的首位址 printf a 1 p n n a 1 表示陣列a 2 第二個...

c c 中三維陣列的傳遞

include include using namespace std const int x 10 const int y 10 const int z 10 int bar double arr y z int main cout foo 0 0 0 endl 1.0 bar foo syste...

一維陣列,二維陣列,三維陣列名的意義

一維陣列 int a 3 a是陣列的位址,所以 a 1是相當於增加12個位元組。a a 對一維陣列的位址取值等於陣列首元素的位址。一維陣列名是指標常量,是首元素的位址。二維陣列 int a 5 5 a i j 的解析 a i 第i 1 個一維陣列的位址 a i 第i 1個一維陣列首元素的位址 a i...