java怎么读取数组中的最后一个数

如题所述

数组中的索引是从0开始算的,那么就简单了,直接读取(数组长度-1)处的元素就是数组中的最后一个数了,代码如下:

public class Test {
public static void main(String[] args) {
int[] a = {1, 3, 5, 6};
System.out.println("数组最后一个数为: " + a[a.length - 1]);
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-11-20

您好,提问者:

    int[] arr = {1,2,3,4,5};
    int next = arr[arr.length-1];
    System.out.println("最后一个为:" + next);

    

本回答被提问者和网友采纳
第2个回答  2014-11-20
int a[]={1,2,3};
a[a.length-1]
第3个回答  2017-03-30
[ ]这里边输入 数组.length()-1
第4个回答  2018-06-27

1、直接通过下标来获取

2、下标的值就是当前数组的长度-1,就可以

3、见图

4、见代码:

/**

* 公司 深圳市海枫科技有限公司

* 创建时间 2018年6月22日

* 邮件 [email protected]

*/

package com.itdugu.unit01.test1;

import java.util.Scanner;

public class Test2 {

public static void main(String[] args) {

String arr[] = {"海枫科技","独孤码农","深圳市海枫科技独孤码农"};

System.out.println("获取数组的最后一个是:"+arr[arr.length-1]);

}

}