<i id='GDFYN'><tr id='GDFYN'><dt id='GDFYN'><q id='GDFYN'><span id='GDFYN'><b id='GDFYN'><form id='GDFYN'><ins id='GDFYN'></ins><ul id='GDFYN'></ul><sub id='GDFYN'></sub></form><legend id='GDFYN'></legend><bdo id='GDFYN'><pre id='GDFYN'><center id='GDFYN'></center></pre></bdo></b><th id='GDFYN'></th></span></q></dt></tr></i><div id='GDFYN'><tfoot id='GDFYN'></tfoot><dl id='GDFYN'><fieldset id='GDFYN'></fieldset></dl></div>

    1. <small id='GDFYN'></small><noframes id='GDFYN'>

      • <bdo id='GDFYN'></bdo><ul id='GDFYN'></ul>
      <legend id='GDFYN'><style id='GDFYN'><dir id='GDFYN'><q id='GDFYN'></q></dir></style></legend>
    2. <tfoot id='GDFYN'></tfoot>

      使用 Java 查找基元数组中的最大值/最小值

      Finding the max/min value in an array of primitives using Java(使用 Java 查找基元数组中的最大值/最小值)
    3. <small id='db1Sk'></small><noframes id='db1Sk'>

        <bdo id='db1Sk'></bdo><ul id='db1Sk'></ul>

          <legend id='db1Sk'><style id='db1Sk'><dir id='db1Sk'><q id='db1Sk'></q></dir></style></legend>
            <i id='db1Sk'><tr id='db1Sk'><dt id='db1Sk'><q id='db1Sk'><span id='db1Sk'><b id='db1Sk'><form id='db1Sk'><ins id='db1Sk'></ins><ul id='db1Sk'></ul><sub id='db1Sk'></sub></form><legend id='db1Sk'></legend><bdo id='db1Sk'><pre id='db1Sk'><center id='db1Sk'></center></pre></bdo></b><th id='db1Sk'></th></span></q></dt></tr></i><div id='db1Sk'><tfoot id='db1Sk'></tfoot><dl id='db1Sk'><fieldset id='db1Sk'></fieldset></dl></div>
            • <tfoot id='db1Sk'></tfoot>
                <tbody id='db1Sk'></tbody>
                本文介绍了使用 Java 查找基元数组中的最大值/最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                写一个函数来确定数组中的最小值/最大值很简单,例如:

                It's trivial to write a function to determine the min/max value in an array, such as:

                /**
                 * 
                 * @param chars
                 * @return the max value in the array of chars
                 */
                private static int maxValue(char[] chars) {
                    int max = chars[0];
                    for (int ktr = 0; ktr < chars.length; ktr++) {
                        if (chars[ktr] > max) {
                            max = chars[ktr];
                        }
                    }
                    return max;
                }
                

                但这不是已经在某个地方完成了吗?

                but isn't this already done somewhere?

                推荐答案

                使用 Commons Lang(转换)+ Collections(转换为最小值/最大值)

                Using Commons Lang (to convert) + Collections (to min/max)

                import java.util.Arrays;
                import java.util.Collections;
                
                import org.apache.commons.lang.ArrayUtils;
                
                public class MinMaxValue {
                
                    public static void main(String[] args) {
                        char[] a = {'3', '5', '1', '4', '2'};
                
                        List b = Arrays.asList(ArrayUtils.toObject(a));
                
                        System.out.println(Collections.min(b));
                        System.out.println(Collections.max(b));
                   }
                }
                

                请注意,Arrays.asList() 包装了底层数组,因此它不应占用太多内存,也不应对数组元素执行复制.

                Note that Arrays.asList() wraps the underlying array, so it should not be too memory intensive and it should not perform a copy on the elements of the array.

                这篇关于使用 Java 查找基元数组中的最大值/最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Java Bytecode Manipulation Library Suggestions(Java 字节码操作库建议)
                Java CLI UI-design: frameworks or libraries?(Java CLI UI 设计:框架还是库?)
                About the use of Beans.xml configuration file in Spring Framework application(关于Spring Framework应用中Beans.xml配置文件的使用)
                What is the difference between Spring, Struts, Hibernate, JavaServer Faces, Tapestry?(Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?)
                Are there any android application framework like spring?(有没有像spring这样的android应用程序框架?)
                Java Swing based game framework. Any advice?(基于 Java Swing 的游戏框架.有什么建议吗?)
                  <tbody id='vrqoE'></tbody>

                    <small id='vrqoE'></small><noframes id='vrqoE'>

                    <i id='vrqoE'><tr id='vrqoE'><dt id='vrqoE'><q id='vrqoE'><span id='vrqoE'><b id='vrqoE'><form id='vrqoE'><ins id='vrqoE'></ins><ul id='vrqoE'></ul><sub id='vrqoE'></sub></form><legend id='vrqoE'></legend><bdo id='vrqoE'><pre id='vrqoE'><center id='vrqoE'></center></pre></bdo></b><th id='vrqoE'></th></span></q></dt></tr></i><div id='vrqoE'><tfoot id='vrqoE'></tfoot><dl id='vrqoE'><fieldset id='vrqoE'></fieldset></dl></div>
                  1. <legend id='vrqoE'><style id='vrqoE'><dir id='vrqoE'><q id='vrqoE'></q></dir></style></legend>

                    • <tfoot id='vrqoE'></tfoot>
                          <bdo id='vrqoE'></bdo><ul id='vrqoE'></ul>