简体   繁体   English

创建一个从 1 到 10 的随机数组。然后,在 java 中将所有 7 移动到数组的前面

[英]Create a random array of numbers from 1 – 10. Then, move all of the 7s to the front of the array in java

Create a random array of numbers from 1 – 10. Then, move all of the 7s to the front of the array.从 1 到 10 创建一个随机数组。然后,将所有 7 移到数组的前面。 The order of the other numbers is not important as long as all numbers follow the group of Lucky 7s.其他号码的顺序并不重要,只要所有号码都跟在幸运 7 组之后即可。 Looking at the code for insertion sort and selection sort might be very helpful for this assignment.查看插入排序和选择排序的代码可能对这个作业很有帮助。 Arrays.toString() will also be useful. Arrays.toString() 也很有用。 For instance, if you are given the list:: 4 5 6 7 1 2 3 7 The list could become – all 7s must be first:: 7 7 6 4 1 2 3 5例如,如果给定列表:: 4 5 6 7 1 2 3 7 列表可能变成 – 所有 7 必须在前面:: 7 7 6 4 1 2 3 5

My code:我的代码:

public class NumberShifter
{

public int[] go(int[] arrayToBeShifted, int index)
{
  int originalArray[] = new int[index];
  int valueBeingMoved = originalArray[index];

  for (int i = index; i > 0; i--) 
  {
    arrayToBeShifted[i] = arrayToBeShifted[i-1];
  }

  arrayToBeShifted[0] = valueBeingMoved;

  return arrayToBeShifted;
}
}

The runner:亚军:

class Main
{
  public static void main(String[] args) 
  {
    int random[] = new int[10];
    for(int i=0; i<random.length; i++)
    {
      random[i] = (int)(Math.random()*6)+1;
    }
    NumberShifter rt = new NumberShifter();   
    System.out.println(rt.go(random,7));
    System.out.println(rt.go(random,7));
    System.out.println(rt.go(random,7));
    System.out.println(rt.go(random,7));
  }
}

This program give me the errors Please anyone tell me that i use a correct method to solve this question.这个程序给我错误请任何人告诉我我使用正确的方法来解决这个问题。 If i use a wrong method for this question please solve this with correct method.如果我对这个问题使用了错误的方法,请用正确的方法解决这个问题。

The problem is here in NumberShifter :问题出在NumberShifter中:

int originalArray[] = new int[index];
int valueBeingMoved = originalArray[index];

You are creating an array with a size of [index] .您正在创建一个大小为[index]的数组。 Suppose index = 7 , so it has index values of 0, 1, 2...6 But in the next line you are accessing originalArray[index] (originalArray[7]) which does not exist.假设index = 7 ,所以它的索引值为 0, 1, 2...6 但是在下一行中,您正在访问不存在的originalArray[index] (originalArray[7]) 。 So you are getting the exception.所以你得到了例外。

Apart from that, your code has other issues as well.除此之外,您的代码还有其他问题。

I would like to suggest this very simple approach:我想建议这种非常简单的方法:

public class Main {

    public static void main(String[] args) {
        int random[] = {4, 7, 6, 5, 7, 8, 0, 7, 1, 7};

        printArray(random);
        move7sToFirst(random);
        printArray(random);
    }

    public static void move7sToFirst(int[] random) {
        for (int i = 0; i < random.length; i++) //traverse array to find 7s
            if (random[i] == 7)                 //if 7 found
                for (int j = 0; j < i; j++)     //traverse from beginning to index before 7 to find non-7
                    if (random[j] != 7)         //if non-7 found
                        swap(random, i, j);     //swap 7 with non-7
    }

    public static void swap(int[] random, int index1, int index2) {
        int temp = random[index1];
        random[index1] = random[index2];
        random[index2] = temp;
    }

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

Output:输出:

4 7 6 5 7 8 0 7 1 7 
7 7 7 7 4 6 5 8 0 1 

Of course, this is the simplest approach with time complexity of O(n^2).当然,这是最简单的方法,时间复杂度为 O(n^2)。 This can be improved by memorizing the indexes for next iterations and time complexity can be reduced significantly to improve it almost as O(n).这可以通过记住下一次迭代的索引来改进,并且可以显着降低时间复杂度以将其改进几乎为 O(n)。

There are multiple inaccuracies in your logic, I have fixed the code and following should solve your problem您的逻辑中存在多个不准确之处,我已修复代码,以下应该可以解决您的问题

public class NumberShifter{
   public static void main(String[] args){
      int[] array = {7,7,1,1,2,3,4,7,7,7,5,7};
      for(int i=0;i<array.length;i++){
         System.out.print(" "+array[i]);
      }
      shift7s(array);
      System.out.println("\n");
      for(int i=0;i<array.length;i++){
         System.out.print(" "+array[i]);
      }
   }

   public static void shift7s(int[] array) {
      int i = 0;
      int j = array.length -1;
      while(i < j){
         i = getIndexOfNextNonSeven(array, i);
         j = getIndexOfNextSeven(array, j);
         if(i == array.length || j < 0) break;
         if(i < j){
            swap(array, i, j);
         }
      }
   }

   private static int getIndexOfNextNonSeven(int[] array, int currentIndex){
      while(currentIndex < array.length &&
              array[currentIndex] == 7 ){
         currentIndex++;
      }
      return currentIndex;
   }

   private static int getIndexOfNextSeven(int[] array, int currentIndex){
      while(currentIndex < array.length &&
              array[currentIndex] != 7 ){
         currentIndex--;
      }
      return currentIndex;
   }

   private static void swap(int[] array, int p1, int p2){
      int temp = array[p1];
      array[p1] = array[p2];
      array[p2] = temp;
   }
}

This is the way i solve the question i make two separate classes runner and main for code.这就是我解决问题的方法,我为代码创建了两个单独的类 runner 和 main。

My code:我的代码:

public class NumberShifter
{

public int[] go(int[] arrayToBeShifted, int index)
{
  for (int i = 0, e = 0; i < arrayToBeShifted.length; i++) 
  {
    if (arrayToBeShifted[i] == 7)
    {
    arrayToBeShifted[i] = arrayToBeShifted[e];
    arrayToBeShifted[e] = 7;
    e++;
    }
  }
  return arrayToBeShifted;
}
}

The runner:亚军:

class Runner
{
  public static void main(String[] args) 
  {
    NumberShifter rt = new NumberShifter();  
  //demo to see  if code works
    System.out.println(java.util.Arrays.toString(rt.go(new int[]{1, 10, 9, 2, 8, 2, 5, 6, 10, 7, 9, 8, 6, 7, 2, 7, 6, 10, 5, 3},7)));

  //random arrays according to question
  int random[] = new int[20];
    for(int i=0; i<random.length; i++)
    {
      random[i] = (int)(Math.random()*10)+1;
    }
     System.out.println(java.util.Arrays.toString(rt.go(random,7)));
    System.out.println(java.util.Arrays.toString(rt.go(random,7)));
    System.out.println(java.util.Arrays.toString(rt.go(random,7)));

  }

}

Create a random array of numbers from 1 – 10. Then, move all of the 7s to the front of the array.创建一个从 1 到 10 的随机数字数组。然后,将所有 7 移到数组的前面。 The order of the other numbers is not important as long as all numbers follow the group of Lucky 7s.其他数字的顺序并不重要,只要所有数字都遵循幸运 7 的组即可。 Looking at the code for insertion sort and selection sort might be very helpful for this assignment.查看插入排序和选择排序的代码可能对这个作业非常有帮助。 Arrays.toString() will also be useful. Arrays.toString() 也很有用。 For instance, if you are given the list :: 4 5 6 7 1 2 3 7 The list could become – all 7s must be first :: 7 7 6 4 1 2 3 5例如,如果给你一个列表 :: 4 5 6 7 1 2 3 7 列表可能变成——所有 7 必须是第一个 :: 7 7 6 4 1 2 3 5

My code:我的代码:

public class NumberShifter
{

public int[] go(int[] arrayToBeShifted, int index)
{
  int originalArray[] = new int[index];
  int valueBeingMoved = originalArray[index];

  for (int i = index; i > 0; i--) 
  {
    arrayToBeShifted[i] = arrayToBeShifted[i-1];
  }

  arrayToBeShifted[0] = valueBeingMoved;

  return arrayToBeShifted;
}
}

The runner:跑者:

class Main
{
  public static void main(String[] args) 
  {
    int random[] = new int[10];
    for(int i=0; i<random.length; i++)
    {
      random[i] = (int)(Math.random()*6)+1;
    }
    NumberShifter rt = new NumberShifter();   
    System.out.println(rt.go(random,7));
    System.out.println(rt.go(random,7));
    System.out.println(rt.go(random,7));
    System.out.println(rt.go(random,7));
  }
}

This program give me the errors Please anyone tell me that i use a correct method to solve this question.这个程序给了我错误请任何人告诉我我使用正确的方法来解决这个问题。 If i use a wrong method for this question please solve this with correct method.如果我对这个问题使用了错误的方法,请用正确的方法解决这个问题。

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

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