數組練習整理

來源:互聯網
上載者:User
/*需求: 定義一個函數接收一個int類型的數組對象,找出數組對象中 的最大元素返回給調用者。*/class Demo8 {    public static void main(String[] args)     {        int[] arr = {-12,-14,-5,-26,-4};        int max = getMax(arr);        System.out.println("最大值:"+ max);     }    public static int  getMax(int[] arr){        int max = arr[0]; //用於記錄最大值        for(int i = 1 ; i < arr.length ; i++){            if(arr[i]>max){  //如果發現有元素比max大,那麼max變數就記錄該元素。                max = arr[i];            }        }        return max;    }}
/*需求: 定義一個函數接收一個int類型的數組對象, 把數組中的最大值放在數組中的第一位。選擇排序(直接排序):使用一個元素與其他 的元素挨個比較一次,符合條件交換位置。*/class Demo9{    public static void main(String[] args)     {        int[] arr = {12,5,17,8,9};  //對於5元素的數組,只需要找出4個最大值就可以排序了。        selectSort(arr);    }    public static void selectSort(int[] arr){        //把最大值放在首位置。        for(int j = 0; j<arr.length-1; j++){  //  控制的是輪數。            for(int i = j+1 ; i<arr.length ; i++){ // 找出最大值                if(arr[i]>arr[j]){                    //交換位置                    int temp = arr[i];                    arr[i] = arr[j];                    arr[j] = temp;                }            }        }/*        //把老二放在第二個位置        for(int i = 2  ; i< arr.length ; i++){            if(arr[i]>arr[1]){                int temp = arr[i];                arr[i] = arr[1];                arr[1] = temp;            }        }        //把老三放在第三個位置        for(int i = 3  ; i< arr.length ; i++){            if(arr[i]>arr[2]){                int temp = arr[i];                arr[i] = arr[2];                arr[2] = temp;            }        }        //把老四放在第四個位置        for(int i = 4  ; i< arr.length ; i++){            if(arr[i]>arr[3]){                int temp = arr[i];                arr[i] = arr[3];                arr[3] = temp;            }        }

/*
需求: 定義一個函數接收一個int類型的數組對象, 把數組中的最大值放在數組中的最後一位。

冒泡排序:冒泡排序的思想就是使用相鄰的兩個 元素挨個比較一次,符合條件交換位置。

*/

class Demo10
{
public static void main(String[] args)
{
int[] arr = {12,8,17,5,9}; // 最大的索引值: 4 容量:5
bubbleSort(arr);
}

public static void bubbleSort(int[] arr){    // 把最大值放在最後一個位置    for(int j = 0 ; j<arr.length-1 ; j++){ //控制輪數        for(int i = 0 ; i<arr.length-1-j  ; i++){  // 找出一個最大值              //相鄰的元素比較            if(arr[i]>arr[i+1]){                int temp  = arr[i];                arr[i] = arr[i+1];                arr[i+1] = temp;            }        }    }

/*

    //把老二放在倒數第二個位置上。    for(int i = 0 ;  i <arr.length-1-1 ; i++){        if(arr[i]>arr[i+1]){            int temp = arr[i];            arr[i] = arr[i+1];            arr[i+1] = temp;        }       }    //把老三放在倒數第三個位置上。    for(int i = 0 ;  i <arr.length-1-2 ; i++){        if(arr[i]>arr[i+1]){            int temp = arr[i];            arr[i] = arr[i+1];            arr[i+1] = temp;        }       }    //把老四放在倒數第四個位置上。    for(int i = 0 ;  i <arr.length-1-3 ; i++){        if(arr[i]>arr[i+1]){            int temp = arr[i];            arr[i] = arr[i+1];            arr[i+1] = temp;        }       }

*/

    //遍曆數組,查看效果    System.out.print("目前的元素:");    for (int i = 0 ; i<arr.length  ;i++){        System.out.print(arr[i]+",");    }}

}


/*
需求:定義一個函數接收一個數組對象和一個要尋找的目標元素,函數要返回該目標元素在
數組中的索引值,如果目標元素不存在數組中,那麼返回-1表示。

折半尋找法(二分法): 使用前提必需是有序的數組。

*/
class Demo12
{
public static void main(String[] args)
{
int[] arr = {12,16,19,23,54};
//int index = searchEle(arr,23);
int index = halfSearch(arr,116);
System.out.println(“元素所在的索引值是:”+ index);
}

public static int halfSearch(int[] arr, int target){    //定義三個變數分別記錄最大、最小、中間的尋找範圍索引值    int max = arr.length-1;    int min = 0;    int mid = (max+min)/2;    while(true){        if(target>arr[mid]){            min = mid+1;        }else if(target<arr[mid]){            max = mid -1;        }else{            //找到了元素            return mid;        }        //沒有找到的情況        if (max<min){            return -1;        }        //重新計算中間索引值        mid = (min+max)/2;    }}public static int searchEle(int[] arr, int target){    for(int i = 0 ; i<arr.length ; i++){        if(arr[i]==target){            return i;        }    }    return -1;}

}


/*
需求: 定義 一個函數接收一個char類型的數組對象,然後翻轉數組中的元素。

char[] arr = {‘a’,’b’,’c’,’d’,’e’};

*/
class Demo13
{
public static void main(String[] args)
{
//System.out.println(“Hello World!”);
char[] arr = {‘a’,’b’,’c’,’d’,’e’};
reverse(arr);

}public static void reverse(char[] arr){    for(int startIndex = 0 ,endIndex = arr.length-1 ;   startIndex<endIndex ; startIndex++,endIndex--){        char temp = arr[startIndex];        arr[startIndex] = arr[endIndex];        arr[endIndex] = temp;    }    //遍曆數組,查看效果    System.out.print("目前的元素:");    for (int i = 0 ; i<arr.length  ;i++){        System.out.print(arr[i]+",");    }}

}


/*
toString();
排序(sort)
找元素在數組中的位置(二分尋找法) binarySearch

數組的工具類(Arrays):

作業1: 自定實現一個Arrays的toString方法。

*/
import java.util.*;
class Demo14
{

public static void main(String[] args) {    int[] arr = {12,3,1,10,8};    //排序的方法    Arrays.sort(arr);      String info = Arrays.toString(arr);    System.out.println("數組的元素:"+ info);    // 1, 3, 8, 10, 12      int index = Arrays.binarySearch(arr,9);// 二分法尋找 : 如果能在數組中找到對應 的元素,那麼就返回該資料的索引值,如果沒有找到那麼就返回一個負數表示。    System.out.println("找到的索引值:"+ index);}

}

“`

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.