简体   繁体   English

打印原始数组未排序的数组

[英]To print original array not sorted array

I am looking to print out my original unsorted array, I have it printing in order and sorted but I can't seem to get the original one to print out unsorted. 我想打印出原始的未排序数组,我将其按顺序打印并排序,但是我似乎无法得到原始的未排序数组。 I have used printRuleAndArray(String rule) and I have also used LengthCompare for the new sorted array, my problem is the original!!! 我用过printRuleAndArray(String rule),我也用过LengthCompare用于新的排序数组,我的问题是原始的!!!

import java.util.*;
import java.util.Arrays;

// Example of how to sort an array
public class Sorting2
{
    //declare an array of strings
    static String[] nameArray = {"Alan", "Peter", "Ed", "Stephen", "Pheadraa"};

    public static void main(String[] args)
    {

        // sorting by length
        Arrays.sort(nameArray, new LengthCompare());
        //print out elements of array
        System.out.println(Arrays.toString(nameArray));
        //count the number of elements in the array
        int counter=nameArray.length;
        //print out numeric number of elements in array
        System.out.println("Number of elements in array: " + counter);
        //print out sorted array with shortest first and longest last
        printRuleAndArray("Sorted list by name length:");

    }

Arrays.sort() will always sort the array you pass into it, it doesn't produce a fresh copy - so if you really need the unsorted array to hang around as well as the sorted array, then you'll have to make a copy of it: Arrays.sort()将始终对传递给它的数组进行排序,它不会产生新的副本-因此,如果您确实需要将未排序的数组以及已排序的数组挂起,则必须制作一个副本:

String copyArr[] = new String[nameArray.length];
System.arraycopy( nameArray, 0, copyArr, 0, nameArray.length );

However, preferable to this approach (if feasible) would just be to do all the operations you need on the unsorted array (such as printing it or converting it to a string), then sort it afterwards. 但是,比这种方法(如果可行)更可取的是仅对未排序的数组执行所需的所有操作(例如打印或将其转换为字符串),然后再对其进行排序。

As pointed out in the comment, Arrays.copyOf() could also be used to accomplish the same thing. 正如评论中指出的那样, Arrays.copyOf()也可以用于完成相同的操作。

Arrays.sort will have altered your original array. Arrays.sort将更改您的原始数组。 Your choices are to either print your original array before sorting it, or to copy your original array and sort the copy. 您的选择是在对原始数组进行排序之前先对其进行打印,或者对原始数组进行复制并对副本进行排序。

Call String unsortedArr = Arrays.toString(nameArray); 调用String unsortedArr = Arrays.toString(nameArray); before array sorting, and when you need to print unsorted array just call System.out.println(unsortedArr); 在数组排序之前,以及当您需要打印未排序的数组时,只需调用System.out.println(unsortedArr);

Arrays.sort() sorts the array you pass into it. Arrays.sort()对传递给它的数组进行排序。 If you would like the original array later, copy the array first and then sort that array. 如果以后需要原始数组,请先复制该数组,然后对该数组进行排序。

 for(String arr : nameArray ) {  //arr gets successively each value in nameArray.
System.out.println(arr);
}

this example is using foreach loop 这个例子使用的是foreach循环

Do something like this 做这样的事情

    String orig = Arrays.toString(nameArray);
    Arrays.sort(nameArray, new LengthCompare());
    String sorted = Arrays.toString(nameArray);

    System.out.println(orig);
    System.out.println(sorted);

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

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