线性表插入(java)

来源:互联网 发布:社交平台源码 编辑:程序博客网 时间:2024/05/22 04:48
import org.junit.Test;/** * 作者:lxg *  * 时间:2015年7月25日 下午2:00:28 *  */public class Test001 {/** * 定义一个长度为6的数组,给前五个赋值,最后一个位置空出来, * 然后判断要插入的位置是否在规定范围内,在范围之内的话从倒数第二个位置上的数据开始到要插入的位置, * 所有数据依次向后移动,然后再把要插入的数据插入到指定位置 *  */@Testpublic void demo1(){int length = 6; //数组长度int[] a= new int[length]; //定义一个长度为6的数组int i = 3; //要插入的位置int e = 10;//要插入的数据for(int temp = 0; temp<length-1; temp++){  //给数组赋值a[temp] = temp+1;}/** * 首先要确定插入的位置在规定的范围之内且不在表尾 */if(i>=1 && i<length){for(int temp=length-2; temp>=i-1; temp--){a[temp+1] = a[temp];}}a[i-1] = e;System.out.println("打印结果:");for(int temp=0;temp<length;temp++){System.out.println(a[temp]);} }




0 1
原创粉丝点击