導航:首頁 > 編程語言 > 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數組逆序輸出相關的資料

熱點內容
文件名修改為文件夾的名字批處理 瀏覽:251
拍照程序員 瀏覽:827
wps怎麼把pdf轉jpg 瀏覽:217
自拍用什麼app做的藝術照 瀏覽:169
h3c無線配置命令 瀏覽:515
linux代碼閱讀工具 瀏覽:160
能夠畫出對稱圖形的是什麼app 瀏覽:424
單片機投票器 瀏覽:467
程序員那麼可愛唱嗎 瀏覽:830
手機誤刪的app怎麼恢復 瀏覽:700
java第三方加密庫 瀏覽:660
編譯代碼軟體哪個好 瀏覽:997
編譯器軟體圖片 瀏覽:880
美團專送app怎麼不接受遠單 瀏覽:833
伺服器mgmt口如何連接電腦 瀏覽:798
做程序員至少要精通幾種 瀏覽:673
個人用雲伺服器價格對比 瀏覽:257
如何遠程刪除伺服器文件夾 瀏覽:779
a9賬號如何移植到安卓 瀏覽:340
gpib介面編程 瀏覽:468