广告
返回顶部
首页 > 资讯 > 精选 >java实现从方法返回多个值功能示例
  • 879
分享到

java实现从方法返回多个值功能示例

java方法返回值 2023-05-30 23:05:16 879人浏览 泡泡鱼
摘要

本文实例讲述了java实现从方法返回多个值功能。分享给大家供大家参考,具体如下:这里介绍三个方法,使java方法返回多个值。方法1:使用集合类方法2:使用封装对象方法3:使用引用传递示例代码如下:import java.util.HashM

本文实例讲述了java实现从方法返回多个值功能。分享给大家供大家参考,具体如下:

这里介绍三个方法,使java方法返回多个值。

方法1:使用集合
方法2:使用封装对象
方法3:使用引用传递

示例代码如下:

import java.util.HashMap;import java.util.Map;public class Test {    public Map<String, Integer> test1(int[] arr) {    Map<String, Integer> map = new HashMap<String, Integer>();    int max = Integer.MIN_VALUE;    int min = Integer.MAX_VALUE;    for (int i = 0; i < arr.length; i++) {      if (arr[i] > max) {        max = arr[i];      }      if (arr[i] < min) {        min = arr[i];      }    }    map.put("max", max);    map.put("min", min);    return map;  }    public Result test2(int[] arr) {    Result result = new Result();    int max = Integer.MIN_VALUE;    int min = Integer.MAX_VALUE;    for (int i = 0; i < arr.length; i++) {      if (arr[i] > max) {        max = arr[i];      }      if (arr[i] < min) {        min = arr[i];      }    }    result.setMax(max);    result.setMin(min);    return result;  }    public int test3(int[] arr, Result result) {    int max = Integer.MIN_VALUE;    int min = Integer.MAX_VALUE;    for (int i = 0; i < arr.length; i++) {      if (arr[i] > max) {        max = arr[i];      }      if (arr[i] < min) {        min = arr[i];      }    }    result.setMax(max);    result.setMin(min);    int total = arr.length;    return total;  }    public static void main(String[] args) {    Test t = new Test();    int[] arr = { 1, 2, 3, 4, 5, 6 };    // ----------方法1测试-----------    // Map<String, Integer> map = t.test1(arr);    // System.out.println("max : " + map.get("max"));    // System.out.println("min : " + map.get("min"));    // ----------方法2测试-----------    // Result result = t.test2(arr);    // System.out.println("max : " + result.getMax());    // System.out.println("min : " + result.getMin());    // ----------方法3测试-----------    Result result = new Result();    int total = t.test3(arr, result);    System.out.println("total : " + total);    System.out.println("max : " + result.getMax());    System.out.println("min : " + result.getMin());  }}class Result {  int max;  int min;  // 构造函数  public Result() {    super();  }  // getters/setters(略)}

--结束END--

本文标题: java实现从方法返回多个值功能示例

本文链接: https://www.lsjlt.com/news/222264.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • java实现从方法返回多个值功能示例
    本文实例讲述了java实现从方法返回多个值功能。分享给大家供大家参考,具体如下:这里介绍三个方法,使java方法返回多个值。方法1:使用集合类方法2:使用封装对象方法3:使用引用传递示例代码如下:import java.util.HashM...
    99+
    2023-05-30
    java 方法 返回值
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作