日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

java实现原数组根据下标分隔成两个子数组并且在原数组中交换两个子数组的位置...

發(fā)布時(shí)間:2023/12/2 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java实现原数组根据下标分隔成两个子数组并且在原数组中交换两个子数组的位置... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

此類實(shí)現(xiàn):
輸出一行數(shù)組數(shù)據(jù),根據(jù)輸入的下標(biāo),以下標(biāo)位置為結(jié)束,將原數(shù)組分割成兩組子數(shù)組。
并交換兩個(gè)子數(shù)組的位置,保持子數(shù)組中的元素序號不變.
如:原數(shù)組為7,9,8,5,3,2 以下標(biāo)3為分割點(diǎn),分割為子數(shù)組一:7,9,8,5。和子數(shù)組二:3,2.
經(jīng)過交換算法后的結(jié)果應(yīng)為:3,2,7,9,8,5

有兩種交換算法
<1>前插法:將子數(shù)組3,2另存在一個(gè)臨時(shí)數(shù)組中,將原數(shù)組7,9,8,5,3,2每一位向后移兩個(gè)位置
??再將子數(shù)組3,2插入到移動(dòng)好元素位置的原數(shù)組中。
<2>逆置法:將原數(shù)組7,9,8,5,3,2逆置為2,3,5,8,9,7
???再分別逆置分割好的兩個(gè)子數(shù)組,結(jié)果應(yīng)為:3,2,7,9,8,5

package 順序表;import java.util.ArrayList; import java.util.Scanner;/*** @param args* @author 劉雁冰* @date 2015-2-2 19:43*//** 此類實(shí)現(xiàn):* 輸出一行數(shù)組數(shù)據(jù),根據(jù)輸入的下標(biāo),以下標(biāo)位置為結(jié)束,將原數(shù)組分割成兩組子數(shù)組。* 并交換兩個(gè)子數(shù)組的位置,保持子數(shù)組中的元素序號不變.* 如:原數(shù)組為7,9,8,5,3,2 以下標(biāo)3為分割點(diǎn),分割為子數(shù)組一:7,9,8,5。和子數(shù)組二:3,2.* 經(jīng)過交換算法后的結(jié)果應(yīng)為:3,2,7,9,8,5* * 有兩種交換算法* <1>前插法:將子數(shù)組3,2另存在一個(gè)臨時(shí)數(shù)組中,將原數(shù)組7,9,8,5,3,2每一位向后移兩個(gè)位置* <2>逆置法:將原數(shù)組7,9,8,5,3,2逆置為2,3,5,8,9,7* 再分別逆置分割好的兩個(gè)子數(shù)組,結(jié)果應(yīng)為:3,2,7,9,8,5*/public class ResetOrderListPostion {/** int []order:數(shù)組存儲(chǔ)用戶輸入的原數(shù)組* int postion:存儲(chǔ)用戶輸入的分隔下標(biāo)*/static int []order;static int postion;/** 前插法*/public void frontInsert(int []orderInsert,int postion){/** 使用ArrayList鏈表來存儲(chǔ)分隔后的子數(shù)組一*/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);//將原數(shù)組每一個(gè)元素往后移動(dòng)子數(shù)組長度次for(int j=orderInsert.length-1;j>0;j--){orderInsert[j]=orderInsert[j-1];}} //將子數(shù)組一插入到移動(dòng)好的子數(shù)組中for(int k=a.length-1;k>=0;k--){orderInsert[k]=a[k];}//注意消除最后一個(gè)元素的,號System.out.println("使用前插法---交換位置后數(shù)組的結(jié)果如下:");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){//逆置整個(gè)原數(shù)組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;}//逆置子數(shù)組一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;}//逆置子數(shù)組二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;}//注意消除最后一個(gè)元素的,號System.out.println("使用逆置法---交換位置后的結(jié)果如下:");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("請輸入數(shù)組,按-1結(jié)束輸入");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("您輸入的數(shù)組數(shù)據(jù)為:");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("請輸入下標(biāo),以此來將原數(shù)組分隔成兩個(gè)數(shù)組(注意,輸入的下標(biāo)不能小于0且不能大于等于數(shù)組長度):");int postion=sc.nextInt();System.out.println("您輸入的分割下標(biāo)為:\n"+postion);//判定輸入的分隔下標(biāo)有效性if(postion<0||postion>=order.length)System.out.println("輸入有誤!");else{System.out.println("********************請選擇數(shù)組位置交換算法********************");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("輸入有誤!");}}}}

  

?

轉(zhuǎn)載于:https://www.cnblogs.com/luckid/p/4268700.html

總結(jié)

以上是生活随笔為你收集整理的java实现原数组根据下标分隔成两个子数组并且在原数组中交换两个子数组的位置...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。