简体   繁体   English

需要帮助在Java中创建缩放列表方法

[英]need help creating a scaling list method in java

I'm trying to create a method called scaleByK that should replace every integer of value k with k copies of itself. 我正在尝试创建一个名为scaleByK的方法,该方法应将其值k的每个整数替换为其自身的k个副本。 For example, if the list is: [2, 4, -2, 5, 3, 0, 7] before the method is invoked, it should be [2, 2, 4, 4, 4, 4, 5, 5, 5, 5, 5, 3, 3, 3, 7, 7, 7, 7, 7, 7, 7 ] after the method executes. 例如,如果列表在调用方法之前是:[2,4,-2,5,5,3,0,7],则它应该是[2,2,4,4,4,4,4,5,5,执行该方法后,再执行5、5、5、3、3、3、7、7、7、7、7、7、7]。 Note that the method should remove from the list all 0s and negative values. 请注意,该方法应从列表中删除所有0和负值。

this is what I have so far 这就是我到目前为止

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

length=7 when the method starts 方法开始时的长度= 7

When I run the method this is what I get 2 2 4 -2 -2 5 3 0 0 7 当我运行该方法时,这就是我得到的2 2 4 -2 -2 5 3 0 0 7

The original list is 2 4 -2 5 3 0 7 原始列表是2 4 -2 5 3 0 7

here is my print method 这是我的打印方法

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

The length resets back to 7 every time I run the program. 每次我运行程序时,长度重置为7。

You're overwriting your values in this array, so it won't work. 您正在覆盖此数组中的值,因此它将不起作用。 You might want to create a second array of the size of the sum of integers and fill it then. 您可能想要创建第二个具有整数总和大小的数组,然后填充它。

Try applying your print() method on each step of iteration to vizualize your algorithm and sort the problems out. 尝试在迭代的每个步骤上应用print()方法,以生动地体现算法并解决问题。

I've got 2 versions here for you, first using ArrayList and then just with arrays: 我这里有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)+" ]");
}

Both versions output the same thing but obviously the first method is preferable, please comment below if you need anything. 两种版本都输出相同的内容,但显然第一种方法更可取,如果需要,请在下面评论。

Also worth a quick mention, List<> structures cannot hold primitive types like int or double or bool , instead they have to be wrapped by classes such as Integer , fortunately as you can see this doesn't have a big impact on what we're doing here. 同样值得一提的是, List<>结构不能容纳诸如intdoublebool类的原始类型,而是必须由诸如Integer类的类包装,幸运的是,您可以看到这对我们的内容没有太大的影响。在这里做。

Just with printing: 只需打印:

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

Replacing the array: 替换数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM