java数组、字符串比较插入及排序问题

package wanlin;
import java.util.*;
public class Wl_insert1 {
    public static void main(String[] args){
        String Books[] = {"Computer","Hibernate","Java","Structs"};
        String newbook ="";
        String newBooks[] = new String[Books.length+1];
        System.out.print("请输入您要插入的书名:");
        Scanner input = new Scanner(System.in);
        newbook = input.nextLine();
        System.out.print("您输入的书名是:"+newbook);
        int index = Books.length,i=0;
        for(i=0;i<Books.length;i++){
            if(Books[i].compareToIgnoreCase(newbook)>=0){
                index = i;
                break;
            }
        }
        System.out.println("\n"+index);
        System.arraycopy(Books,0,newBooks,0,Books.length);
        System.out.print("插入前的书目录:");
        System.out.println(Arrays.toString(newBooks));
            
        for(i=Books.length;i>index;i--){
             newBooks[i] = newBooks[i-1];
        }
        System.out.print("后移之后的书目录:");
        System.out.println(Arrays.toString(newBooks));
        newBooks[index] = newbook;
        System.out.print("插入后的书目录:");
        System.out.println(Arrays.toString(newBooks));
     }
}



    运行结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要定义一个二叉堆类,包含堆的初始化、插入、删除等方法。然后,将字符数组插入到堆中,最后依次从堆中删除元素,即可得到排序后的字符数组。 以下是一个示例代码: ```java public class BinaryHeap { private String[] heapArray; private int maxSize; private int currentSize; public BinaryHeap(int maxSize) { this.maxSize = maxSize; this.currentSize = 0; this.heapArray = new String[maxSize]; } public boolean isEmpty() { return currentSize == 0; } public boolean insert(String value) { if (currentSize == maxSize) { return false; } heapArray[currentSize] = value; trickleUp(currentSize++); return true; } public String remove() { String root = heapArray[0]; heapArray[0] = heapArray[--currentSize]; trickleDown(0); return root; } private void trickleUp(int index) { int parent = (index - 1) / 2; String bottom = heapArray[index]; while (index > 0 && heapArray[parent].compareTo(bottom) < 0) { heapArray[index] = heapArray[parent]; index = parent; parent = (parent - 1) / 2; } heapArray[index] = bottom; } private void trickleDown(int index) { int largerChild; String top = heapArray[index]; while (index < currentSize / 2) { int leftChild = 2 * index + 1; int rightChild = leftChild + 1; if (rightChild < currentSize && heapArray[leftChild].compareTo(heapArray[rightChild]) < 0) { largerChild = rightChild; } else { largerChild = leftChild; } if (top.compareTo(heapArray[largerChild]) >= 0) { break; } heapArray[index] = heapArray[largerChild]; index = largerChild; } heapArray[index] = top; } } public class HeapSort { public static void main(String[] args) { String[] array = {"apple", "banana", "orange", "pear", "peach"}; BinaryHeap heap = new BinaryHeap(array.length); for (String s : array) { heap.insert(s); } for (int i = 0; i < array.length; i++) { array[i] = heap.remove(); } for (String s : array) { System.out.print(s + " "); } } } ``` 在这个示例中,我们首先创建了一个二叉堆类 `BinaryHeap`,包含堆的初始化、插入、删除等方法。然后,我们创建了一个字符数组,并将其插入到堆中。最后,我们依次从堆中删除元素,并将其存储到数组中,即可得到排序后的字符数组

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值