有一个数组 int[] ls = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };请问,如何使数组下标 逢“3”减1呢?就是当下标=3,就把当前的数去掉。 

解决方案 »

  1.   


    for(int i=0;i<ls.length;i++){
        if(i=>3){
           ls[i] = ls[i+1]
        }
    }
      

  2.   

    不知道我理解的对不对?对于我程序里的数组,过滤掉索引时3,6,9的元素,也就是4,7和10。public class Replace {
    public static void main(String args[]){
    int[] intArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    int length = intArray.length - intArray.length/3;

    int[] target = new int[length];

    int count = 0;

    for(int i=0; i<intArray.length; i++){
    if(i%3 == 0 && i != 0)
    continue;

    target[count] = intArray[i];
    count ++;
    }

    for(int i : target)
    System.out.print(i +" ");
        }
    }
    输出:
    1 2 3 5 6 8 9 
      

  3.   


     public static void main(String[] args) {
            int[] ls = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int[] result = new int[ls.length - ls.length / 3];
            for (int i = 1,j=0; i <= ls.length; i++) {
                if (0 == i % 3) {
                    continue;
                } else {
                    result[j++] = ls[i - 1];
                }
            }
            // 输出去掉指定位数的数组
            for (int i = 0; i < result.length; i++) {
                System.out.println(result[i]);
            }
        }
      

  4.   

    public static void DelData(){
            int[] ls = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9};
            int lslength = ls.length;
            int [] newls = new int [lslength - ((lslength - 1) / 3)];
            
            for(int i = 0 , j = 0 ; i < lslength ; i++){
                if(i == 0 || i % 3 != 0)
                    newls[j++] = ls[i];
            }
        }不是很明白你说的意思。
    我这里写的是当数组下标是3,6,9......时将这个元素去掉。