日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

Java黑皮书课后题第7章:**7.19(是否排好序了?)编写以下方法,如果参数中的list数组已经排好序了则返回true。编写一个测试程序,提示用户输入一个列表,显示该列表是否已经排好序

發(fā)布時(shí)間:2024/7/23 java 25 豆豆

7.19(是否排好序了?)編寫(xiě)以下方法,如果參數(shù)中的list數(shù)組已經(jīng)排好序了則返回true。編寫(xiě)一個(gè)測(cè)試程序,提示用戶(hù)輸入一個(gè)列表,顯示該列表是否已經(jīng)排好序

  • 題目
    • 題目描述
    • 破題
  • 代碼

題目

題目描述

7.19(是否排好序了?)編寫(xiě)以下方法,如果參數(shù)中的list數(shù)組已經(jīng)排好序了則返回true:
public static boolean isSorted(int[] list)
編寫(xiě)一個(gè)測(cè)試程序,提示用戶(hù)輸入一個(gè)列表,顯示該列表是否已經(jīng)排好序
注意,輸入的第一個(gè)數(shù)表示列表中的元素個(gè)數(shù),該數(shù)不是列表的一部分
運(yùn)行示例:

Enter the size of the list: 8 Enter the contents of the list: 10 1 5 16 61 9 11 1 The list has 8 integers 10 1 5 16 61 9 11 1 The list is not sorted Enter the size of the list: 10 Enter the contents of the list: 1 1 3 4 4 5 7 9 11 21 The list has 10 integers 1 1 3 4 4 5 7 9 11 21 The list is already sorted Enter the size of the list: 10 Enter the contents of the list: 10 9 8 7 6 5 4 3 2 1 The list has 10 integers 10 9 8 7 6 5 4 3 2 1 The list is already sorted

破題

  • 主方法:獲取用戶(hù)輸入(定義list長(zhǎng)度)
  • 主方法:聲明一個(gè)數(shù)組+獲取用戶(hù)輸入(給list賦值)
  • 主方法:調(diào)用方法isSorted,傳入剛剛賦值好的數(shù)組,接收boolean返回值
  • 主方法:根據(jù)返回值輸出結(jié)果
  • isSorted方法:獲取傳入的數(shù)組長(zhǎng)度為length
  • isSorted方法:聲明一個(gè)數(shù)組,長(zhǎng)度等于length
  • isSorted方法:將輸入的數(shù)組復(fù)制給剛剛聲明的數(shù)組
  • isSorted方法:對(duì)list進(jìn)行排序(直接調(diào)用Arrays類(lèi)的sort方法)
  • isSorted方法:對(duì)兩個(gè)數(shù)組進(jìn)行深度比較(equals(lis1, list2)),并直接輸出返回值
  • 代碼

    import java.util.Arrays; import java.util.Scanner;public class Test7_19 {public static void main(String[] args) {//1. 主方法:獲取用戶(hù)輸入(定義list長(zhǎng)度)Scanner input = new Scanner(System.in);System.out.print("Enter the size of the list: ");int length = input.nextInt();//2. 主方法:聲明一個(gè)數(shù)組+獲取用戶(hù)輸入(給list賦值)int[] list = new int[length];System.out.print("Enter the contents of the list: ");for (int i = 0 ; i < length ; i++){list[i] = input.nextInt();}//2+ 輸出數(shù)組長(zhǎng)度和元素System.out.print("The list has " + length + " integers ");for (int i = 0 ; i < length ; i++){System.out.print(list[i] + " ");}//3. 主方法:調(diào)用方法isSorted,傳入剛剛賦值好的數(shù)組,接收boolean返回值boolean bool = isSorted(list);//4. 主方法:根據(jù)返回值輸出結(jié)果if (bool)System.out.print("\nThe list is already sorted");elseSystem.out.print("\nThe list is not sorted");}// isSorted方法:判斷傳入的數(shù)組是否已經(jīng)排好序public static boolean isSorted(int[] list){//5. isSorted方法:獲取傳入的數(shù)組長(zhǎng)度為lengthint length = list.length;//6. isSorted方法:聲明2個(gè)數(shù)組,長(zhǎng)度等于lengthint[] list_copy = new int[length];int[] list_copy_reverse = new int[length];//7. isSorted方法:將輸入的數(shù)組復(fù)制給剛剛聲明的兩個(gè)數(shù)組(順序不同)for (int i = 0 ; i < length ; i ++){list_copy[i] = list[i];list_copy_reverse[i] = list[length - i - 1];}//8. isSorted方法:對(duì)list進(jìn)行排序(直接調(diào)用Arrays類(lèi)的sort方法)Arrays.sort(list);//9. isSorted方法:對(duì)兩個(gè)數(shù)組進(jìn)行深度比較(equals(lis1, list2)),并直接輸出返回值return Arrays.equals(list, list_copy) || Arrays.equals(list, list_copy_reverse);} } 與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖

    總結(jié)

    以上是生活随笔為你收集整理的Java黑皮书课后题第7章:**7.19(是否排好序了?)编写以下方法,如果参数中的list数组已经排好序了则返回true。编写一个测试程序,提示用户输入一个列表,显示该列表是否已经排好序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。