1

我正在尝试创建一个名为 scaleByK 的方法,该方法应将值 k 的每个整数替换为自身的 k 个副本。例如,如果在调用方法之前列表是:[2, 4, -2, 5, 3, 0, 7],则应该是[2, 2, 4, 4, 4, 4, 5, 5, 5, 5, 5, 3, 3, 3, 7, 7, 7, 7, 7, 7, 7] 方法执行后。请注意,该方法应从列表中删除所有 0 和负值。

这就是我到目前为止所拥有的

public void scaleByK(){
      for(int i=0;i<length;i++){
        if(list[i]<0||list[i]==0){
          for(int j=i+1;j<length;j++){
            list[j-1]=list[j];
          }
          length-=1;
        }
        else{
          for(int k=i;k<list[k]-1+i;k++){
            for(int x=length-1; x>k;x--){
              list[x+list[k]-1]=list[x];
            }
            for(int g=k+1; g<list[k+i];g++){
              list[g]=list[k];
            }
          }
          i=list[i]-1+i;
          length+=list[i]-1;
        }
      }
    }

方法开始时长度=7

当我运行该方法时,这就是我得到的 2 2 4 -2 -2 5 3 0 0 7

原始列表是 2 4 -2 5 3 0 7

这是我的打印方法

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

每次我运行程序时,长度都会重置回 7。

4

3 回答 3

0

只需打印:

public static void scaleByK(int in[]){
        for(int a = 0; a < in.length; a++){
            if(in[a] > 0){
                for(int b = 0; b < in[a]; b++){
                    System.out.println(in[a]);
                }
            }
        }
}

更换阵列:

public static int[] scaleByK(int in[]){
        int length = 0;
        for(int a = 0; a < in.length; a++){
            if(in[a] > 0){
                length = length + in[a];
            }
        }
        int temp[] = new int[length];
        int count = 0;
        for(int b = 0; b < in.length; b++){
            if(in[b] > 0){
                for(int c = 0; c < in[b]; c++){
                    temp[count] = in[b];
                    count++;
                }
            }
        }
        in = new int[temp.length];
        for(int d = 0; d < temp.length; d++){
            in[d] = temp[d];
        }
        return in;
    }
于 2013-10-26T20:39:08.640 回答
0

我在这里为您准备了 2 个版本,首先使用ArrayList,然后仅使用数组:

public void scaleBy(int[] ints) {

    ArrayList<Integer> newInts = new ArrayList<Integer>();
    String myList = "";

    for (Integer integer : ints) {

        if (integer > 0) {

            for (int i = integer; i > 0; i--) {
                newInts.add(integer);
                myList += integer+", ";
            }
        }
    }

    System.out.println("[ "+myList.substring(0, myList.length() - 2)+" ]");
}

public void scaleByArray(int[] ints) {

    int[][] arrays = new int[10][];
    int length = 0;
    int arrayCount = 0;
    String myList = "";

    for (int integer : ints) {

        if (integer > 0) {
            length += integer;
            int[] temp = new int[integer];

            Arrays.fill(temp, integer);

            arrays[arrayCount] = temp;
            arrayCount++;
        }
    }


    int[] master = new int[length];
    int position = 0;

    for (int[] array : arrays) {
        if (array == null)
            break;

        for (int i = 0; i < array.length; i++) {
            master[position] = array[i];
            position++;
            myList += array[i]+", ";
        }
    }

    System.out.println("[ "+myList.substring(0, myList.length() - 2)+" ]");
}

两个版本输出相同的东西,但显然第一种方法更可取,如果你需要什么,请在下面评论。

还值得一提的是,List<>结构不能保存原始类型,如intordoublebool,相反它们必须由类包装Integer,幸运的是,您可以看到这对我们在这里所做的事情没有太大影响。

于 2013-10-26T19:04:29.963 回答
0

您正在覆盖此数组中的值,因此它不起作用。您可能想要创建第二个整数和大小的数组,然后填充它。

尝试将您的print()方法应用于迭代的每个步骤,以可视化您的算法并解决问题。

于 2013-10-26T18:50:58.300 回答