按数字拆分数组的功能不起作用

时间:2014-11-19 17:27:03

标签: java

我必须执行一个函数splitArrayByNum,它将给出一个整数数组和一个整数分隔符,它将使用分隔符拆分数组。此函数将返回2D数组:例如,splitArrayByNum([0,0,0,3,1,1,1], 3) -> [[0,0,0],[1,1,1]]splitArrayByNum([3,1,3,3], 3) ->[[1]]

为什么我的代码不能在控制台上打印任何内容?

package hw3;
import java.util.Arrays;

public class ArrayUtils {

    public static void p(int[][] a)
    {
        for (int i = 0; i < a.length; i++)
        {
            System.out.println(Arrays.toString(a[i]));
        }
    }

    public static void main(String[] args)
    {
         int[] x1={0,0,0,3,1,1,1}; int y1=3;
         splitArrayByNum(x1, y1);
         p(splitArrayByNum(x1, y1));
    }

    public static int lastIndexOf(int value, int[] nums) {
        for (int i = nums.length - 1; i >=0; i--) {
            if (nums[i] == value) {
                return i;
            }
        }
        return -1;
    }
    public static int firstIndexOf(int value, int[] nums) {
        int index = -1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == value) {
                index = i;
                break;
            }
        }
        return index;
    }
   \\ turns the number of elements between two Extreme instances
    public static int mySpan(int value, int[] nums) {
        if (lastIndexOf(value, nums)==-1)
            return 0;
        else
            return lastIndexOf(value, nums) - firstIndexOf(value, nums) + 1;
    }
    public static int [][] splitArrayByNum(int[]input,int number){
        //finding the'm' in 'arraySplit[m][]'
        int []arrayInUse=new int[input.length];
        arrayInUse=Arrays.copyOfRange(input, 0, input.length);
        int counter=0;
        while(mySpan(number,arrayInUse)!=0)
        {
            int first=arrayInUse[firstIndexOf(number,arrayInUse)];
            int last= arrayInUse[lastIndexOf(number,arrayInUse)];
            int k=1;
            int m=mySpan(number,arrayInUse);
            while (m>0)
            {        \\ checking for deleting sequences  from the sides 

                if( (arrayInUse[first]!=arrayInUse[first+k]) &&(arrayInUse[last]!=arrayInUse[last-k]))
                    continue;
                if (arrayInUse[first]==arrayInUse[first+k])
                    m=m-1;
                if(m<0)
                    continue;
                first=first+k;
                if ((arrayInUse[last])==(arrayInUse[last-k]))
                    m=m-2;
                if(m<0)
                    continue;
                last=last-k;
                k=k+1;

            }
            if (m<0)
                continue;
            counter=counter+1;
            arrayInUse=Arrays.copyOfRange(arrayInUse, first, last+1);
        }
        int [][]splitArray=new int[counter][];
        // creating splitArray
        counter=0;
        arrayInUse=Arrays.copyOfRange(input, 0, input.length);
        while(mySpan(number,arrayInUse)!=0)
        {
            int first=arrayInUse[firstIndexOf(number,arrayInUse)];
            int last= arrayInUse[lastIndexOf(number,arrayInUse)];
            int k=1;
            int m=mySpan(number,arrayInUse);
            while (m>0)
            {        \\ checking for deleting sequences  from the sides 
                if( (arrayInUse[first]!=arrayInUse[first+k]) &&(arrayInUse[last]!=arrayInUse[last-k]))
                    continue;
                if (arrayInUse[first]==arrayInUse[first+k])
                    m=m-1;
                if(m<0)
                    continue;
                first=first+k;
                if ((arrayInUse[last])==(arrayInUse[last-k]))
                    m=m-2;
                if(m<0)
                    continue;
                last=last-k;
                k=k+1;

            }
            if (m<0)
                continue;

            arrayInUse = Arrays.copyOfRange(arrayInUse, first+1, last+1);
            splitArray[counter] = Arrays.copyOfRange(arrayInUse,first+1,firstIndexOf(number,arrayInUse));
            counter = counter+1;
        }

        return splitArray;
    }
}

1 个答案:

答案 0 :(得分:0)

while (m>0)的第一个区块永远不会退出,因为您的两个条件m从未被满足。

        while (m>0)
        { 
            System.out.println(m); //Debugging statement to show that value of m never changes

            if( (arrayInUse[first]!=arrayInUse[first+k]) &&(arrayInUse[last]!=arrayInUse[last-k]))
                continue;
            if (arrayInUse[first]==arrayInUse[first+k])
                m=m-1;
            if(m<0)
                continue;
            first=first+k;
            if ((arrayInUse[last])==(arrayInUse[last-k]))
                m=m-2;
            if(m<0)
                continue;
            last=last-k;
            k=k+1;

        }