Pair数据类型(Java实现)

本文使用Java构建了一个基于范型的pair内部类,在这一内部类的基础上实现了寻找数组的最大值和最小值,并以pair数据结构输出,同时实现了寻找字符串按首字母排序的第一个字符串以及最后一个字符串,并以pair数据结构输出。本程序使用到的Java相关技术为:

  1. Java泛型;
  2. Java内部类;
  3. Java增强for循环;

本程序由两个文件组成,GeneticProgramming.java定义了内部类和相关方法,testMain.java对类文件进行了测试,GeneticProgramming.java源码如下:

package com.Genetic_Programming.java;public class GeneticProgramming {
    public Pair<String> Str_minmax(String[] a)
    {
        if(a == null || a.length == 0)
        {
            return null;
        }
        String min = a[0];
        String max = a[0];
        for(int i = 1;i<a.length;i++)
        {
            if(min.compareTo(a[i])>0)
            {
                min = a[i];
            }
            if(max.compareTo(a[i])<0)
            {
                max = a[i];
            }
        }
        System.out.println("min is "+min+" , max is "+max);
        return new Pair<String>(min, max);
    }public Pair<Double> Num_minmax(double[] a)
    {
        Pair<Double> pair = new Pair<Double>(Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY);
        if(a.length == 0)
        {
            System.out.println("array is empty.");
        }
        else if(a.length == 1)
        {
            System.out.println("array length is 1. min = max = "+a[0]);
        }
        else
        {
            for(Double element:a)
            {
                if(element<pair.first)
                {
                    pair.setFirst(element);
                }
                if(element>pair.second)
                {
                    pair.setSecond(element);
                }
            }
            System.out.println("min number and max number is " + pair.first+" , "+pair.second);
        }
        return pair;
    }
    public static class Pair<T>
    {
        private T first;
        private T second;public Pair()
        {
            first = null;
            second = null;
        }public Pair(T first,T second)
        {
            this.first = first;
            this.second = second;
        }public T getFirst()
        {
            return first;
        }
        public T getSecond()
        {
            return second;
        }public void setFirst(T newFirst)
        {
            first = newFirst;
        }public void setSecond(T newSecond)
        {
            second = newSecond;
        }
    }
}

testMain.java源码如下:

package com.Genetic_Programming.java;public class testMain {
    public static void main(String[] args)
{
        GeneticProgramming test = new GeneticProgramming();
        double[] testNumArray = {1,3,5,8,10.2,112,69};
        String[] testStrArray = {"banana","apple","orange","peach","watermelon","pineapple"};
        test.Num_minmax(testNumArray);
        test.Str_minmax(testStrArray);
    }
}

运行结果如下:

/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJIDEA.app/Contents/lib/idea_rt.jar=49687:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/liangqian/IdeaProjects/Java范型/out/production/Java范型 com.Genetic_Programming.java.testMain
min number and max number is 1.0 , 112.0
min is apple , max is watermelon



全文完。

如果您想了解更多C++/Java/机器学习相关的知识,欢迎扫描下方的二维码,关注“梁公子的备忘录”,每天一篇相关的技术分享,期待与您一起学习,共同进步!

在这里插入图片描述

你可能感兴趣的:(Java)