1

我正在尝试编写一个将从二维数组中删除一列的类,但我一直遇到我不理解的错误。我想我在这里误解了一些非常基本的东西,任何帮助将不胜感激

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();
    }

}


}
4

3 回答 3

0

Your difficulties are arising due to using variables outside of their scope. In java, variables basically only exist within the most immediate pair of braces from which they were declared. So, for example:

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
    }

}

The problem you are having is because you are declaring a new 2-d array for each iteration of the for loop and writing one column to it, before throwing that away, creating a new array, and repeating the process.

于 2013-04-27T05:14:46.643 回答
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();
        }       
    }
}
于 2013-04-27T04:17:16.143 回答
0

您不能从静态上下文访问非静态变量,您需要更改int temp;为,static int temp;或者您可以static从方法声明中删除。

您在方法中声明了您的nargs数组coldel,因此无法从其他方法访问它。这意味着这不起作用:

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也需要是静态的(同样,您也可以从中static删除coldel

于 2013-04-27T04:05:33.960 回答