2013-04-27 143 views
1

我想寫一個類,它將從2d數組中刪除一列,但我一直遇到我不明白的錯誤。我想我誤解的東西非常基本在這裏,任何幫助,將不勝感激從二維數組中刪除一列

public class CollumnSwitch 
{ 
int[][] matrix; 
int temp; 

public static void coldel(int[][] args,int col) 
{ 
    for(int i =0;i<args.length;i++) 
    { 
     int[][] nargs = new int[args.length][args[i].length-1]; 
     for(int j =0;j<args[i].length;j++) 
     { 
      if(j!=col) 
      { 
       int temp = args[i][j]; 
      } 
      nargs[i][j]= temp; 
     } 
    } 
} 

public void printArgs() 
{ 
    for(int i =0;i<nargs.length;i++) 
    { 
     for(int j =0;j<nargs[i].length;j++) 
     { 
      System.out.print(nargs[i][j]); 
     } 
     System.out.println(); 
    } 

} 


} 
+1

你會得到什麼錯誤? – MAV 2013-04-27 03:47:44

+0

看起來像你有確定範圍的問題;使用不存在的變量;並有一個你似乎沒有使用的實例變量矩陣。 – Supericy 2013-04-27 03:56:46

+0

主要的兩個錯誤說,它無法找到我的Nargs數組的符號。而非靜態變量temp不能從靜態上下文中引用。 – 2013-04-27 03:57:42

回答

0

不能從靜態上下文訪問一個非靜態變量,你需要改變int temp;static int temp;或者你可以從你的方法刪除static宣言。

您在coldel方法中聲明瞭nargs數組,因此無法從其他方法訪問該數組。這意味着這不起作用:

for(int i =0;i<nargs.length;i++) //You try to access nargs which is not possible. 
{ 
    for(int j =0;j<nargs[i].length;j++) 
    ... 

也許你希望它是matrix數組你有你的課嗎?就像這樣:

coldel

matrix= new int[args.length][args[i].length-1]; 

printArgs

for(int i =0;i<matrix.length;i++) 
{ 
    for(int j =0;j<matrix[i].length;j++) 
    ... 

這需要matrix是靜態也(同樣,你也可以卸載staticcoldel

0

你可以這樣試試:

static int[][] nargs; 

public static void deleteColumn(int[][] args,int col) 
{ 
    if(args != null && args.length > 0 && args[0].length > col) 
    { 
     nargs = new int[args.length][args[0].length-1]; 
     for(int i =0;i<args.length;i++) 
     { 
      int newColIdx = 0; 
      for(int j =0;j<args[i].length;j++) 
      { 
       if(j!=col) 
       { 
        nargs[i][newColIdx] = args[i][j]; 
        newColIdx++; 
       }    
      } 
     } 
    }  
} 

public static void printArgs() 
{ 
    if(nargs != null) 
    { 
     for(int i =0;i<nargs.length;i++) 
     { 
      for(int j =0;j<nargs[i].length;j++) 
      { 
       System.out.print(nargs[i][j] + " "); 
      } 
      System.out.println(); 
     }  
    } 
} 
0

由於使用範圍之外的變量,您的困難已經出現。在java中,變量基本上只存在於聲明它們的最直接的一對括號中。因此,舉例來說:

public class Foo { 
    int classVar; // classVar is visible by all code within this class 

    public void bar() { 
     classVar = classVar + 1; // you can read and modify (almost) all variables within your scope 
     int methodVar = 0; // methodVar is visible to all code within this method 
     if(methodVar == classVar) { 
      int ifVar = methodVar * classVar; // ifVar is visible to code within this if statement - but not inside any else or else if blocks 
     for(int i = 0; i < 100; i++) { 
      int iterationVar = 0; // iterationVar is created and set to 0 100 times during this loop. 
            // i is only initialized once, but is not visible outside the for loop 
     } 
     // at this point, methodVar and classVar are within scope, 
     // but ifVar, i, and iterationVar are not 
    } 

    public void shoo() { 
     classVar++; // shoo can see classVar, but no variables that were declared in foo - methodVar, ifVar, iterationVar 
    } 

} 

您所遇到的問題是因爲你宣佈一個新的2-d陣列的for循環的每個迭代,寫一列到它,扔那走之前,創建一個新的數組,並重復該過程。