「数组右移n位java」数组向右移动一位

博主:adminadmin 2022-12-12 07:12:05 74

今天给各位分享数组右移n位java的知识,其中也会对数组向右移动一位进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

用java怎么做这道题字符串右移的问题? 字符串右移n位,例如 "hello world" 右移两位 后ldhello wor

public class MoveString { public static void main(String[] args) { String str = "hello world"; int step = 2; System.out.println(moveString(str, step)); } private static String moveString(String str, final int step) { if (str == null || step 0) { return null; } int len = str.length(); return new StringBuilder(str.substring(len - step % len)).append(str.substring(0, len - step % len)).toString(); } } 追问: 我怎么看不懂是什么意思呢? 回答: 你这个向右移几位其实就相当于将字符串最后的几位补到前面去...比如说你移动两位,其实就是将"hello world"最后的两位"ld"放到最前面 String类中的substring方法 就是用来进行字符串截取的

希望采纳

Java 怎样移动数组的位置~比如1 2 3 4 5 6 移动M位,比如2位,变成3 4 5 6 1 2

import java.util.Scanner;

public class ShuZu_yiwei{

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner(System.in);

System.out.println("请输入原数组的数组元素,每个数组元素之间用空格隔开:");

String s = input.nextLine();

String []str = s.split(" ");//把字符串s按照空格划分,然后存储到数组str中

System.out.println("请输入要移动的数组元素个数:");

int M = input.nextInt();

String []array = new String[M];

String []barray = new String[str.length-M];

String []carray = new String[str.length];

System.arraycopy(str, 0, array, 0, M);//把原数组的前面M个复制到array中

System.arraycopy(str, M, barray, 0, barray.length);//把原数组的后str.length-M个复制到array中

System.arraycopy(barray,0,carray,0,barray.length);//把原数组的后str.length-M拷贝到目标数组中的前0到str.length-M,

System.arraycopy(array,0,carray,barray.length,array.length);//把原数组前面M个元素拷贝目标数组的后str.length-M到str.length

System.out.println("移动后的数组为:");

for(int i=0;icarray.length;i++)//打印目标数组的每一个元素

{

System.out.print(carray[i]+" ");

if((i+1)%10==0){//每行输出是个数组元素

System.out.println();

}

}

}

}

Java 数组数据移动?

public static void main(String[] arg){Scanner sc = new Scanner(System.in);System.out.println("请输入数组长度(建议5~10)");int count = 0;while (!e68a847a6431333433626437(count 0)){if (sc.hasNextInt()) {count = sc.nextInt();sc.nextLine();if (count = 0){System.out.println("请输入大于0的数字");}} else {System.out.println("不是数字");}}String[] str = null;while (str == null || str.length != count){System.out.println("请输入"+ count +"个数字,用空格隔开");if (sc.hasNextLine()){str = sc.nextLine().split("\\s+");}}int move = 0;System.out.println("请输入向右移动的位数");while (!(move 0)){if (sc.hasNextInt()) {move = sc.nextInt();if (move = 0){System.out.println("请输入大于0的数字");}} else {System.out.println("不是数字");}}for (int i = 0; i count; i++){System.out.print(str[(count + i - move%count)%count] + " ");}}

JAVA数组移位

我看不懂,你到底是在平移数组元素还是把指定下标的元素变成默认值?第五个元素原来是4你怎么把它变成0了?

下标越界是肯定的。数组一旦被定义,长度就是不变的。与其说是平移,你这个其实是插入操作,在指定的下标插入默认值。一旦插入新元素,数组的元素个数会变长,但这是不允许的,所以肯定会有下标越界异常。

如果你真的是要插入一个默认元素的话你无法在原来的数组上进行操作,只能声明一个新的数组,长度比原来数组长1,然后把旧的元素复制到新的数组,插入默认值,然后再把array的引用指向新数组。可以这样:

int[] array={0,1,2,3,4,5,6,7,8,9};

int[] temp=new int[array.length+1]; //

int k; //k为指定下标,假设已知

for(int i=0;ik;i++)

temp[i]=array[i]; //复制下标前的元素

temp[k]=0; //设置默认值

for(int i=k;iarray.length;i++)

temp[i+1]=array[i]; //复制下标后的元素

array=temp; //改变数组引用

关于数组右移n位java和数组向右移动一位的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-12,除非注明,否则均为首码项目网原创文章,转载请注明出处。