从2d数组中删除列

时间:2013-04-27 03:45:48

标签: java arrays

我正在尝试编写一个将从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();
    }

}


}

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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();
        }       
    }
}

答案 2 :(得分: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
    }

}

你遇到的问题是因为你为for循环的每次迭代声明一个新的2-d数组并向其写入一列,然后抛弃它,创建一个新数组,并重复该过程。