简体   繁体   English

编写此函数的更好方法是什么?

[英]What's the better way to write this function?

I have to go through an array and print out whether it's sorted or not, and if it isn't - print out the first two elements that are not sorted (eg: 6 > 5). 我必须遍历一个数组并打印出它是否已排序,如果不是,则打印出未排序的前两个元素(例如:6> 5)。

So I was wondering what would be considered the best way to do this with a function: 因此,我想知道使用函数执行此操作的最佳方法是什么:

  1. Make a void function which prints out either "It's sorted" or "It's not sorted" and if it's not it prints out the two elements as well. 创建一个void函数,该函数打印出“已排序”或“未排序”,如果不是,则也打印出两个元素。
  2. Make a boolean function which prints out the two unsorted elements (if there are any) and eventually returns either true or false. 创建一个boolean函数,该函数打印出两个未排序的元素(如果有的话),并最终返回true或false。 Then depending on what the function returns, print out whether the array is sorted in the main . 然后根据函数返回的内容,打印出数组是否在main排序。
  3. Do everything in the main as this could be not appropriate for a function? main所有内容都可能不适合某个功能吗?
  4. Some other way? 其他方式?

Note that I'm not asking for any help regarding my assignment itself! 请注意,我并不是在寻求有关作业本身的任何帮助!

I would create a function that returns the index of the last sorted cell, so if it is the last cell then everything is sorted and if it is smaller than you can easily find also the next element and print in main. 我将创建一个返回最后排序的单元格索引的函数,因此,如果它是最后一个单元格,则将对所有内容进行排序,并且如果它小于您可以轻松找到下一个元素并在main中打印的话。 I don't think that functions should have side affects (like print) when they are made for other purpose, and a function like this one that returns index is what you can use: 我不认为将函数用于其他目的时应该具有副作用(例如打印),并且可以使用这样的返回索引的函数:

private int getFirstSortedIndex(int[] numbers){
   int index = 0;
   //find and return the first sorted index;
   return index;
}

public static void main(String[] args){
   int[] numbers;
   //get the array from user input or arguments
   int index = getFirstSortedElement(numbers);
   if( index < numbers.length-1){
      //print numbers[index] and numbers[index+1]
   } else{
      //print "it's sorted"
   }
}

I would create a method that returns the index of the first element that is not correctly ordered: 我将创建一个方法,该方法返回未正确排序的第一个元素的索引:

/**
 * @param array ordered array
 * @return the index of the first unordered element, or -1 if the array is ordered
 */
static int isOrdered(int[] array) {
    // iterate through the array
    //     if a two sequential elements are not ordered, return the index of the first element
    // if all is ok, return -1
}

And then do: 然后执行:

int unsortedIndex = isOrdered(array);
if (unsortedIndex != -1) {
    System.out.println(array[unsortedIndex] + ", " + array[unsortedIndex + 1]);
}

This is better than printing the elements directly in isOrdered() , because now you can reuse isOrdered() in other parts of the code if you need to. 这比直接在isOrdered()打印元素更好,因为现在您可以根据需要在代码的其他部分重用isOrdered()

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

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