有数组a[n]用java代码将数组元素顺序颠倒
2个回答
展开全部
public static void main(String[] args) {
//String a[] = new String[12];
String a[] = new String[11];
// 赋值a[n]
System.out.print("a[n]元素初始值依次为:");
for (int i = 0; i < a.length; i++) {
a[i] = String.valueOf(i);
System.out.print(a[i] + " ");
}
System.out.println(""); // 换行
int length = a.length;
// a[n]元素顺序倒转
System.out.print("a[n]元素倒转后依次为:");
for (int i = 0; i < length/2; i++) {
String tmp = a[i];
a[i]=a[length-1-i];
a[length-1-i]=tmp;
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
//String a[] = new String[12];
String a[] = new String[11];
// 赋值a[n]
System.out.print("a[n]元素初始值依次为:");
for (int i = 0; i < a.length; i++) {
a[i] = String.valueOf(i);
System.out.print(a[i] + " ");
}
System.out.println(""); // 换行
int length = a.length;
// a[n]元素顺序倒转
System.out.print("a[n]元素倒转后依次为:");
for (int i = 0; i < length/2; i++) {
String tmp = a[i];
a[i]=a[length-1-i];
a[length-1-i]=tmp;
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询