导航:首页 > 编程语言 > java数组逆序输出

java数组逆序输出

发布时间:2023-09-05 16:32:10

Ⅰ 将一个数组逆序输出(用java编写)

import java.util.*;
public class test5 {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.print("请输入数组的长度:");
int num=input.nextInt();
int[] score=new int[num];
for(int i=0;i<num;i++){
System.out.print(i+1+":");
score[i]=input.nextInt();
}
System.out.println("\n逆序后为:");
for(int i = 0;i < num/2-1 ;i++){
int temp = score[i];
score[i] = score[num-1-i];
score[num-1-i] = temp;
}
for(int i=0;i<num;i++){
System.out.println(score[i]);
}
}
}

Ⅱ 用JAVA编写一个程序,对数组中每个元素赋值,然后按逆序输出。

public class ArraySort {

/**
* @param args
*/
public static void main(String[] args) {
int[] a = { 12, 2, 45, 23, 9, 88, 33, 23, 22, 5, 4, 4, 5, 1, 9, 7, 2,
7, 8, 0 };
ArraySort.bubbleSort(a);
}

/**
* 冒泡排序。从大到小排序。<br>
*
* @param source
* @return
*/
public static int[] bubbleSort(int[] source) {
boolean isSort = false; // 是否排序

for (int i = 1; i < source.length; i++) {
isSort = false; // 在每次排序前都初始化为false
for (int j = 0; j < source.length - i; j++) {
if (source[j] < source[j + 1]) {
int temp = source[j];
source[j] = source[j + 1];
source[j + 1] = temp;

isSort = true; // 为TRUE表明此次循环(外层循环)有排序。
}
}

if (!isSort)
break; // 如果没有排序,说明数据已经排序完毕。
// 输出每个子循环排序后的数组中的元素
printArray(source, i);
}
return source;
}

/**
* 循环输出数组中的元素。
*
* @param a
* @param idx
* ,第一层循环的index
*/
public static void printArray(int[] a,int idx) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + (i != a.length-1?",":""));
}
System.out.println("--idx:" + idx);
}
}

Ⅲ 数组的逆序输出,java

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("请输入数组大小:");
int n=scan.nextInt();
int[] arr=new int[n];
System.out.println("请输入"+n+"个整数");
for(int i=0;i<n;i++)
{
arr[i]=scan.nextInt();
}
nixu(arr);
print(arr);
}
public static void nixu(int [] arr)
{
for(int sta=0,end=arr.length-1;sta<end;sta++,end--)
{
int temp=arr[sta];
arr[sta]=arr[end];
arr[end]=temp;
}
}
public static void print(int [] arr)
{
System.out.println("逆序输出");
for(int x=0;x<arr.length;x++)
{
System.out.print(arr[x]+",");
}
}
}
你看一下这样行不行,nixu是排序方法,print是打印数组方法,获取键盘输入直接写在主方法里的

Ⅳ 用java 拜托了 将一个数组逆序输出

这个可以使用数组工具类Arrays的asList方法将数组变成List集合,然后使用集合工具类Collections的方法reverse方法将集合中的元素顺序反转,在将集合转换成数组,遍历数组打印即可。
public class Test
{

public static void main(String[] args)
{
String[] s={"a","b","c","d","e","f"};
List<String> list=Arrays.asList(s);
Collections.reverse(list);
list.toArray(s);
for(String str:s)
{
System.out.print(str+" ");
}
}

}

如果满意,请采纳谢谢。

Ⅳ 在java中定义一个方法,使一个整数数组逆序输出。求指导

假设已经有一个数组is,类型为int
for (int i = is.length - 1; i > -1; ++i) {
System.out.println(is[i]);
}

阅读全文

与java数组逆序输出相关的资料

热点内容
哪种加密算法是非对称算法 浏览:989
多文件夹加序号 浏览:844
编译程序包含汇编程序吗 浏览:895
游戏源码搭建一条龙 浏览:192
宋金pdf 浏览:807
服务器为什么需要内存池 浏览:526
php与jquery开发实例 浏览:289
编程大世界故事漫画 浏览:983
北漂程序员出车祸 浏览:914
亚马逊为什么用云端服务器 浏览:65
程序员审核职位 浏览:385
德龙空调压缩机 浏览:780
红旗app如何注册新账户 浏览:360
惯导pdf 浏览:606
c程序员的平均工资 浏览:58
微小店源码 浏览:801
编译原理答题题库 浏览:169
ubuntu编程入门 浏览:301
antbuild命令 浏览:771
怎么订阅服务器 浏览:593