java: Arrays sort

 

GPTs are GPTs: An Early Look at the Labor Market Impact Potential of Large Language Models
https://arxiv.org/pdf/2303.10130.pdf

Sparks of Artificial General Intelligence: Early experiments with GPT-4
https://arxiv.org/abs/2303.12712
https://arxiv.org/pdf/2303.12712.pdf

 

/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述: 1-100 累加值 但要求跳过所有包含有3的数
 * 公用类库 公共所需要用的操作函数或方法
 *
 * 历史版本: JDK 8.01
 * 2023-03-12 创建者 geovindu
 * 2023-03-12 添加 Lambda
 * 2023-03-12 修改:date
 * 公共类
 * 2023-03-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc IntegerDigitControl.java
 * Interface
 * Record
 * Annotation
 * Enum
 * */

package Common;


import java.util.*;


public class NumberAssociativeCotrol {

    /**
     *只显示数组
     * @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];
            }
            //Arrays.sort(duArrList);
            return  duArrList;
        }
        else
        {
            duArrList = new int[0];
            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;
                    }

                }
            }

            //Arrays.sort(duArrList); 直接更简单
            return  duArrList;
        }
        else
        {
            duArrList = new int[0];
            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;
                }
            }

            //Arrays.sort(duArrList);
            return  duArrList;
        }
        else
        {
            duArrList = new int[0];
            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());

        }
        //Arrays.sort(duArrList);
        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;
    }



}

  

/**
 * 版权所有 2023 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *
 * 历史版本: JDK 8.01
 * 2023-03-12 创建者 geovindu
 * 2023-03-12 添加 Lambda
 * 2023-03-12 修改:date
 * 接口类
 * 2023-03-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc NumberAssociativeDal.java
 * Interface
 * Record
 * Annotation
 * Enum
 * */


package Dal;


import Common.NumberAssociativeCotrol;

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

/**
 *整数数组
 * @author geovindu
 * @version  1.0
 */
public class NumberAssociativeDal {

    /**
     *
     */
    NumberAssociativeCotrol numberAssociativeCotrol=new NumberAssociativeCotrol();
    /**
     *
     */
    public  void getNumberDisplay()
    {

        int [] arr=new int[]{1,3,9,5,6,7,13,15,4,8};
        int[] display=numberAssociativeCotrol.getNumberDisplay(arr);

    }

    /**
     *
     * @param arr
     */
    public  int[]  getNumberDisplay(int [] arr)
    {
        //int [] arr=new int[]{1,3,9,5,6,7,13,15,4,8};
        int[] display=numberAssociativeCotrol.getNumberDisplay(arr);
       return display;
    }

    /**
     * 从大到小
     * @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 = numberAssociativeCotrol.getNumberDesc(arr);

            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 =numberAssociativeCotrol.getNumberAsc(arr);
            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)
        {

            duArrList=numberAssociativeCotrol.getNumberArryListAsc(arr);

        }
        return duArrList;
    }

    /**
     *从大到小
     * @param arr 输入数组 整数数组
     * @return 返回 要显示的整数数组
     */
    public List<Integer> getNumberArryListDesc(int [] arr)
    {

        List<Integer> duArrList=new ArrayList<>();
        if(arr.length>0)
        {

                duArrList=numberAssociativeCotrol.getNumberArryListAsc(arr);


        }
        return duArrList;
    }


}

/**
 * 版权所有 2023 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *
 * 历史版本: JDK 8.01
 * 2023-03-12 创建者 geovindu
 * 2023-03-12 添加 Lambda
 * 2023-03-12 修改:date
 * 接口类
 * 2023-03-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc NumberAssociativeDal.java
 * Interface
 * Record
 * Annotation
 * Enum
 * */


package Bll;

import  Dal.NumberAssociativeDal;

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

/**
 *整数数组
 * @author geovindu
 * @version  1.0
 */
public class NumberAssociativeBll {


    /**
     *
     */
    NumberAssociativeDal numberAssociativeDal=new NumberAssociativeDal();
    /**
     *只显示数组
     */
    public void getNumberDisplay()
    {
        int [] arr=new int[]{1,3,9,5,6,7,13,15,4,8};
        System.out.println(Arrays.toString(numberAssociativeDal.getNumberDisplay(arr)));

    }

    /**
     *从大到小
     */
    public void getNumberDesc()
    {

        int [] arr=new int[]{1,3,9,5,6,7,13,15,4,8};
        int[] display=numberAssociativeDal.getNumberDesc(arr);
        System.out.println(Arrays.toString(display));

    }

    /**
     *从小到大
     */
    public void getNumberAsc()
    {

        int [] arr=new int[]{1,33,49,5,6,7,13,15,4};
        int[] display=numberAssociativeDal.getNumberAsc(arr);
        System.out.println(Arrays.toString(display));

    }

    /**
     *从小到大
     */
    public void getNumberArryListAsc()
    {

        int [] arr=new int[]{1,33,49,5,6,7,13,15,4};
        int [] sort=new int[arr.length];
        List<Integer> display=numberAssociativeDal.getNumberArryListAsc(arr);
        int j=0;
        for(Integer i:display) {
            sort[j]=i;
            j++;
        }
        System.out.println(Arrays.toString(sort));

    }

    /**
     *从大到小
     */
    public void getNumberArryListDesc()
    {

        int [] arr=new int[]{1,33,49,5,6,7,13,15,4};
        int [] sort=new int[arr.length];
        List<Integer> display=numberAssociativeDal.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]

  

 

 

 

posted @ 2023-03-25 15:50  ®Geovin Du Dream Park™  阅读(20)  评论(0编辑  收藏  举报