此類實現:
輸出一行數組數據,根據輸入的下標,以下標位置為結束,將原數組分割成兩組子數組。
并交換兩個子數組的位置,保持子數組中的元素序號不變.
如:原數組為7,9,8,5,3,2 以下標3為分割點,分割為子數組一:7,9,8,5。和子數組二:3,2.
經過交換算法后的結果應為:3,2,7,9,8,5
有兩種交換算法
<1>前插法:將子數組3,2另存在一個臨時數組中,將原數組7,9,8,5,3,2每一位向后移兩個位置
??再將子數組3,2插入到移動好元素位置的原數組中。
<2>逆置法:將原數組7,9,8,5,3,2逆置為2,3,5,8,9,7
???再分別逆置分割好的兩個子數組,結果應為:3,2,7,9,8,5
package 順序表;import java.util.ArrayList;
import java.util.Scanner;/*** @param args* @author 劉雁冰* @date 2015-2-2 19:43*//** 此類實現:* 輸出一行數組數據,根據輸入的下標,以下標位置為結束,將原數組分割成兩組子數組。* 并交換兩個子數組的位置,保持子數組中的元素序號不變.* 如:原數組為7,9,8,5,3,2 以下標3為分割點,分割為子數組一:7,9,8,5。和子數組二:3,2.* 經過交換算法后的結果應為:3,2,7,9,8,5* * 有兩種交換算法* <1>前插法:將子數組3,2另存在一個臨時數組中,將原數組7,9,8,5,3,2每一位向后移兩個位置* <2>逆置法:將原數組7,9,8,5,3,2逆置為2,3,5,8,9,7* 再分別逆置分割好的兩個子數組,結果應為:3,2,7,9,8,5*/public class ResetOrderListPostion {/** int []order:數組存儲用戶輸入的原數組* int postion:存儲用戶輸入的分隔下標*/static int []order;static int postion;/** 前插法*/public void frontInsert(int []orderInsert,int postion){/** 使用ArrayList鏈表來存儲分隔后的子數組一*/ArrayList<Integer> listA=new ArrayList<Integer>();for(int i=postion+1;i<orderInsert.length;i++){listA.add(orderInsert[i]);}int a[]=new int[listA.size()];for(int i=0;i<listA.size();i++){a[i]=listA.get(i);//將原數組每一個元素往后移動子數組長度次for(int j=orderInsert.length-1;j>0;j--){orderInsert[j]=orderInsert[j-1];}} //將子數組一插入到移動好的子數組中for(int k=a.length-1;k>=0;k--){orderInsert[k]=a[k];}//注意消除最后一個元素的,號System.out.println("使用前插法---交換位置后數組的結果如下:");for(int j=0;j<orderInsert.length;j++){if(j==orderInsert.length-1)System.out.print(orderInsert[j]);elseSystem.out.print(orderInsert[j]+",");}}/** 逆置法*/public void inversion(int []orderInversion,int postion){//逆置整個原數組for(int i=0;i<orderInversion.length/2;i++){int t=orderInversion[i];orderInversion[i]=orderInversion[orderInversion.length-1-i];orderInversion[orderInversion.length-1-i]=t;}//逆置子數組一for(int i=0;i<(orderInversion.length-postion)/2;i++){int t=orderInversion[i];orderInversion[i]=orderInversion[orderInversion.length-postion-2-i];orderInversion[orderInversion.length-postion-2-i]=t;}//逆置子數組二for(int i=0;i<(postion+1)/2;i++){int t=orderInversion[orderInversion.length-1-i];orderInversion[orderInversion.length-1-i]=orderInversion[orderInversion.length-postion-1+i];orderInversion[orderInversion.length-postion-1+i]=t;}//注意消除最后一個元素的,號System.out.println("使用逆置法---交換位置后的結果如下:");for(int i=0;i<orderInversion.length;i++){if(i==orderInversion.length-1)System.out.print(orderInversion[i]);elseSystem.out.print(orderInversion[i]+",");}System.out.println();}public static void main(String[] args) {// TODO Auto-generated method stubResetOrderListPostion rp=new ResetOrderListPostion();System.out.println("請輸入數組,按-1結束輸入");ArrayList<Integer>list=new ArrayList<Integer>();Scanner sc=new Scanner(System.in);int m=sc.nextInt();while(m!=-1){list.add(m);m=sc.nextInt();}int []order=new int[list.size()];for(int i=0;i<list.size();i++){order[i]=list.get(i);}System.out.println("您輸入的數組數據為:");for(int i=0;i<order.length;i++){if(i==order.length-1)System.out.print(order[i]);elseSystem.out.print(order[i]+",");}System.out.println();System.out.println("請輸入下標,以此來將原數組分隔成兩個數組(注意,輸入的下標不能小于0且不能大于等于數組長度):");int postion=sc.nextInt();System.out.println("您輸入的分割下標為:\n"+postion);//判定輸入的分隔下標有效性if(postion<0||postion>=order.length)System.out.println("輸入有誤!");else{System.out.println("********************請選擇數組位置交換算法********************");System.out.println("********************1--:前插法********************");System.out.println("********************2--:逆置法********************");int n=sc.nextInt();switch(n){case 1:{rp.frontInsert(order, postion);break;}case 2:{rp.inversion(order, postion);break;}default:System.out.println("輸入有誤!");}}}}
?
轉載于:https://www.cnblogs.com/luckid/p/4268700.html
總結
以上是生活随笔為你收集整理的java实现原数组根据下标分隔成两个子数组并且在原数组中交换两个子数组的位置...的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。