标签:返回值 stat insert turn 输出 .so index arrays 插入
import java.util.Arrays; public class Demo4{ public static void main(String[] args) { int[] a = {1,3,8,10,12}; a = insert(a,11); for (int num :a ) { System.out.print(num+" "); } System.out.println(); } //返回值 int[] //参数:int[] a,int num public static int[] insert(int[] a,int num){ // a = Arrays.sort(a); if(num>a[a.length-1]){ return a; } int index = -1; for (int i=0;i<a.length ;i++ ) { if(num>=a[i]&&num<=a[i+1]){ index = i+1; break; } } a = Arrays.copyOf(a,a.length+1); for (int i = a.length-1;i>index;i--) { a[i]=a[i-1]; } a[index] = num; return a; } }
标签:返回值 stat insert turn 输出 .so index arrays 插入
原文地址:https://www.cnblogs.com/yangshuyuan1009/p/9859104.html