java将数组的第一个移到最后的位置,数组长度不变

2024-11-13 21:24:22
推荐回答(5个)
回答1:

public Object[] move(Object[] a) {
List b = new ArrayList(Arrays.asList(a));
Iterator iter=b.iterator();
String first = iter.next();
iter.remove();
b.add(first);
Object[] result = b.toArray();
System.out.println(Arrays.toString(result));
return result;
}

回答2:

关注一下,

第二个人达到了你的要求,

第三个人用了LIST,应该是最简单的,

看你想要怎么样了.

有问题可以给我发消息,

回答你<将设已定的日期增加100天,使用java代码>的朋友.

回答3:

public class Program
{
public static void main(String[] args)
{
move(new String[] { "aaa","bbb","ccc","ddd","eee"});
}

public static void move(String[] strs)
{
String[] newStrs = new String[strs.length];
String temp = strs[0];
for (int i = 0; i < newStrs.length-1; i++)
{
newStrs[i] = strs[i + 1];
}
newStrs[newStrs.length - 1] = temp;

for (int i = 0; i < newStrs.length; i++)
{
System.out.println(newStrs[i]); }

}
}

回答4:

public class Array3 {
public static void main(String[] args) {
String[] Xname=new String[5];
Xname[0]="aaa";
Xname[1]="bbb";
Xname[2]="ccc";
Xname[3]="ddd";
Xname[4]="eee";
change(Xname);
}
static void change(String[] s){
String str=s[0];
for (int i = 0; i < s.length-1; i++) {
s[i]=s[i+1];
}
s[s.length-1]=str;
for (int i = 0; i < s.length; i++) {
System.out.println("Xname["+i+"]=\""+s[i]+"\"");
}
change(s);
}
}

回答5:

public class ArrayMove {
public static void main(String[] args ) {
String Xname[] = new String[5];
Xname[0] = "aaa";
Xname[1] = "bbb";
Xname[2] = "ccc";
Xname[3] = "ddd";
Xname[4] = "eee";

if (Xname.length > 1) {
for (int i = 1, n = 0; i < Xname.length; i++, n++) {
String tmp = Xname[n];
Xname[n] = Xname[i];
Xname[i] = tmp;

System.out.println(Arrays.toString(Xname));
}
}
}
}