集册 Java实例教程 在特定子对象的末尾插入元素

在特定子对象的末尾插入元素

欢马劈雪     最近更新时间:2020-01-02 10:19:05

391
将特定子数组末尾的元素插入其前面数组其余部分的排序位置


//package com.nowjava;/*from NowJava.com - 时  代  Java*/

import java.util.ArrayList;


public class Main {

    /**

     * inserts the element at the end of a particular sub-array into its sorted position in the rest of the array before it

     *  

     * @param arrayList the ArrayList with an element that you want to insert in order

     * @param sortedStart the index of the first element in the sorted subset of the array. all elements between this and

     *          $unsortedStart are sorted

     * @param unsortedStart the index of arrayList that marks the element that you want to insert into the sorted section.

     *         it could already be in the right place, but we're not sure.

     */

    public static <T extends Comparable<? super T>> void insertInOrder(

            ArrayList<T> arrayList, int sortedStart, int unsortedStart) {

        T insertElement = arrayList.get(unsortedStart);

        int insertIndex = unsortedStart;

        while (insertIndex != sortedStart

                && insertElement.compareTo(arrayList.get(insertIndex - 1)) == -1) {

          
展开阅读全文