简体   繁体   中英

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. 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. Note that the method should remove from the list all 0s and negative values.

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

When I run the method this is what I get 2 2 4 -2 -2 5 3 0 0 7

The original list is 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.

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.

I've got 2 versions here for you, first using ArrayList and then just with arrays:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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