如何重新排列数组以使空值占据数组的开头?

我不确定如何将空元素添加到数组的前面。


有没有办法在不反转数组的情况下做到这一点?或许有具体的方法?


public static void main(String[] args) {

    String[] stringArray = new String[]{"one", "two", "three"};

    method(stringArray);

}


public static String[] method(String[] stringArray) {

    String[] biggerStringArray = Arrays.copyOf(stringArray, stringArray.length * 2);


    for (int i = 0; i < biggerStringArray.length; i++) {

        System.out.print(biggerStringArray[i] + ", ");


    }


    return biggerStringArray;

}

我希望结果看起来像:


空,空,空,一,二,三,


代替 :


一、二、三、空、空、空


慕容708150
浏览 165回答 4
4回答

交互式爱情

public static void main(String[] args) {&nbsp; &nbsp; String[] stringArray = new String[] {"one", "two", "three"};&nbsp; &nbsp; method(stringArray);&nbsp; }&nbsp; public static String[] method(String[] stringArray) {&nbsp; &nbsp; String[] biggerStringArray = new String[stringArray.length * 2];&nbsp; &nbsp; for (int i = 0; i < stringArray.length; i++) {&nbsp; &nbsp; &nbsp; biggerStringArray[i + biggerStringArray.length / 2] = stringArray[i];&nbsp; &nbsp; }&nbsp; &nbsp; for (int i = 0; i < biggerStringArray.length; i++) {&nbsp; &nbsp; &nbsp; System.out.print(biggerStringArray[i] + ", ");&nbsp; &nbsp; }&nbsp; &nbsp; return biggerStringArray;&nbsp; }编辑:刚刚记得您还可以使用System.arraycopy:public static String[] method(String[] stringArray) {&nbsp; &nbsp; String[] biggerStringArray = new String[stringArray.length * 2];&nbsp; &nbsp; System.arraycopy(stringArray, 0, biggerStringArray, biggerStringArray.length / 2, stringArray.length);&nbsp; &nbsp; for (int i = 0; i < biggerStringArray.length; i++) {&nbsp; &nbsp; &nbsp; System.out.print(biggerStringArray[i] + ", ");&nbsp; &nbsp; }&nbsp; &nbsp; return biggerStringArray;&nbsp; }

红糖糍粑

您可以首先创建一个长度等于stringArray. 然后,您可以stringArray使用任何 Java 数组连接方法将 与新的空数组连接起来。这是一个例子public static String[] method(String[] stringArray) {&nbsp; &nbsp; String[] nullArray = new String[stringArray.length];&nbsp; &nbsp; String[] biggerStringArray = Stream.of(nullArray, stringArray).flatMap(Stream::of).toArray(String[]::new);&nbsp; &nbsp; // for (int i = 0; i < biggerStringArray.length; i++) {&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp;System.out.print(biggerStringArray[i] + ", ");&nbsp; &nbsp; // }&nbsp; &nbsp; return biggerStringArray;}

临摹微笑

据我所知,不存在执行您正在寻找的操作的单个语句。但这并不意味着您不能自己做。有很多方法可以交换数组中的元素。这是一个:String[] biggerStringArray = Arrays.copyOf(stringArray, stringArray.length * 2);for(int i = 0; i < stringArray.length; i++){&nbsp; &nbsp; biggerStringArray[i] = biggerStringArray[i + stringArray.length];&nbsp; &nbsp; biggerStringArray[i + stringArray.length] = null;}

ITMISS

public static String[] method(String[]stringArray) {&nbsp; &nbsp; String[] biggerStringArray = Arrays.copyOf(stringArray, stringArray.length * 2);&nbsp; &nbsp; List<String> biggerStringList = Arrays.asList(biggerStringArray);&nbsp; &nbsp; biggerStringList.sort(Comparator.nullsFirst(String::compareTo));&nbsp; &nbsp; System.out.println(biggerStringList);&nbsp; &nbsp; return biggerStringList.toArray(new String[biggerStringList.size()]);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java