java: Arrays sort

 

package Dal;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *整數數組
 * @author geovindu
 * @version  1.0
 */
public class NumberAssociative {


    /**
     *只顯示數組
     * @param arr 輸入數組 整數數組
     * @return 返回 要顯示的整數數組
     */
    public int [] getNumberDisplay(int [] arr)
    {
        //int [] arr=new int[]{1,3,9,5,6,7,1,5,4,8};
        //int [] sort=new int[arr.length];
        int[] duArrList;
        if(arr.length>0) {
            duArrList = new int[arr.length];
            for (int i = 0; i <= arr.length - 1; i++) {
                duArrList[i] = arr[i];
            }

            return  duArrList;
        }
        else
        {
            duArrList = new int[1];
            return duArrList;
        }

    }

    /**
     * 從大到小
     * @param arr  輸入數組 整數數組
     * @return 返回 要顯示的整數數組
     */
    public int [] getNumberDesc(int [] arr)
    {
        //int [] arr=new int[]{1,3,9,5,6,7,1,5,4,8};
       // int [] sort=new int[arr.length];
        int[] duArrList;

        if(arr.length>0)
        {
            duArrList = new int[arr.length];
            for (int i = 0; i <= arr.length - 1; i++) {
                duArrList[i] = arr[i];
            }
            int temp = Integer.MAX_VALUE;
            for (int i = 0; i < duArrList.length - 1; i++) {
                for (int j = 0; j < duArrList.length - 1 - i; j++) {
                    if (duArrList[j] < duArrList[j + 1]) {
                        temp = duArrList[j];
                        duArrList[j] = duArrList[j + 1];
                        duArrList[j + 1] = temp;
                    }

                }
            }
            return  duArrList;
        }
        else
        {
            duArrList = new int[1];
            return duArrList;
        }

    }

    /**
     * 從小到大
     * @param arr  輸入數組 整數數組
     * @return 返回 要顯示的整數數組
     */
    public int [] getNumberAsc(int [] arr)
    {
        int[] duArrList;
        Boolean flag=false;
        if(arr.length>0)
        {
            duArrList = new int[arr.length];
            for (int i = 0; i <= arr.length - 1; i++) {
                duArrList[i] = arr[i];
            }
            int temp = Integer.MIN_VALUE;
            for (int i = 0; i < duArrList.length - 1; i++) {
                for (int j = 0; j < duArrList.length - 1 - i; j++) {
                    if (duArrList[j] > duArrList[j + 1]) {
                        temp = duArrList[j];
                        duArrList[j] = duArrList[j + 1];
                        duArrList[j + 1] = temp;
                        flag=true;
                    }

                }
                if(!flag) {
                    break;
                }
            }
            return  duArrList;
        }
        else
        {
            duArrList = new int[1];
            return duArrList;
        }

    }

    /**
     *從小到大
     * @param arr 輸入數組 整數數組
     * @return 返回 要顯示的整數數組
     */
    public List<Integer> getNumberArryListAsc(int [] arr)
    {

        List<Integer> duArrList=new ArrayList<>();
        if(arr.length>0)
        {
            for(int i:arr)
            {
                duArrList.add(i);

            }
            Collections.sort(duArrList, Comparator.naturalOrder());

        }
        return duArrList;
    }

    /**
     *從大到小
     * @param arr 輸入數組 整數數組
     * @return 返回 要顯示的整數數組
     */
    public List<Integer> getNumberArryListDesc(int [] arr)
    {

        List<Integer> duArrList=new ArrayList<>();
        if(arr.length>0)
        {
            for(int i:arr)
            {
                duArrList.add(i);

            }
            //Collections.sort(duArrList, Comparator.naturalOrder()); // 升
            Collections.sort(duArrList, Comparator.reverseOrder()); //降

        }
        return duArrList;
    }
}



package Bll;

import  Dal.NumberAssociative;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 *整數數組
 * @author geovindu
 * @version  1.0
 */
public class NumberAssociativeBll {

    /**
     *只顯示數組
     */
    public void getNumberDisplay()
    {
        NumberAssociative numberAssociative=new NumberAssociative();
        int [] arr=new int[]{1,3,9,5,6,7,13,15,4,8};
        int[] display=numberAssociative.getNumberDisplay(arr);
        System.out.println(Arrays.toString(display));

    }

    /**
     *從大到小
     */
    public void getNumberDesc()
    {
        NumberAssociative numberAssociative=new NumberAssociative();
        int [] arr=new int[]{1,3,9,5,6,7,13,15,4,8};
        int[] display=numberAssociative.getNumberDesc(arr);
        System.out.println(Arrays.toString(display));

    }

    /**
     *從小到大
     */
    public void getNumberAsc()
    {
        NumberAssociative numberAssociative=new NumberAssociative();
        int [] arr=new int[]{1,33,49,5,6,7,13,15,4};
        int[] display=numberAssociative.getNumberAsc(arr);
        System.out.println(Arrays.toString(display));

    }

    /**
     *從小到大
     */
    public void getNumberArryListAsc()
    {
        NumberAssociative numberAssociative=new NumberAssociative();
        int [] arr=new int[]{1,33,49,5,6,7,13,15,4};
        int [] sort=new int[arr.length];
        List<Integer> display=numberAssociative.getNumberArryListAsc(arr);
        int j=0;
        for(Integer i:display) {
            sort[j]=i;
            j++;
        }
        System.out.println(Arrays.toString(sort));

    }

    /**
     *從大到小
     */
    public void getNumberArryListDesc()
    {
        NumberAssociative numberAssociative=new NumberAssociative();
        int [] arr=new int[]{1,33,49,5,6,7,13,15,4};
        int [] sort=new int[arr.length];
        List<Integer> display=numberAssociative.getNumberArryListDesc(arr);
        int j=0;
        for(Integer i:display) {
            sort[j]=i;
            j++;
        }
        System.out.println(Arrays.toString(sort));

    }
}

  

 

 

 

調用:

 

        //整數數組排序
        NumberAssociativeBll numberAssociativeBll=new NumberAssociativeBll();
        numberAssociativeBll.getNumberDisplay();
        numberAssociativeBll.getNumberAsc();
        numberAssociativeBll.getNumberDesc();
        numberAssociativeBll.getNumberArryListAsc();
        numberAssociativeBll.getNumberArryListDesc();

  

 

輸出

[1, 3, 9, 5, 6, 7, 13, 15, 4, 8]
[1, 4, 5, 6, 7, 13, 15, 33, 49]
[15, 13, 9, 8, 7, 6, 5, 4, 3, 1]
[1, 4, 5, 6, 7, 13, 15, 33, 49]
[49, 33, 15, 13, 7, 6, 5, 4, 1]

  

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章