[转载] Java:获取数组中的子数组的多种方法

参考链接: Java中的数组Array

我的个人博客:zhang0peter的个人博客 

 

Java:从一个数组中创建子数组 

使用Arrays.copyOfRange函数 

Arrays.copyOfRange支持:boolean[], byte[] ,char[],double[],float[],int[],long[]以及泛型的 T[] 使用示例如下: 

import java.util.Arrays;

 

public class hello {

    public static void main(String[] args) {

        int[] src = new int[]{1, 2, 3, 4, 5};

        int newArray[] = Arrays.copyOfRange(src, 0, 2);

        for (int i : newArray) {

            System.out.println(i);

        }

    }

}

 

 

官方文档如下: 

copyOfRange

public static <T> T[] copyOfRange(T[] original,

                                  int from,

                                  int to)

Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case null is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from.

The resulting array is of exactly the same class as the original array.

 

Parameters:

    original - the array from which a range is to be copied

from - the initial index of the range to be copied, inclusive

to - the final index of the range to be copied, exclusive. (This index may lie outside the array.)

Returns:

    a new array containing the specified range from the original array, truncated or padded with nulls to obtain the required length

Throws:

    ArrayIndexOutOfBoundsException - if from < 0 or from > original.length()

    IllegalArgumentException - if from > to

    NullPointerException - if original is null

Since:

    1.6

 

使用subList 

对于List来说,可以使用subList获取子列表 注意:subList返回的是原列表的一个视图,它所有的操作最终都会作用在原列表上 示例如下: 

import java.util.ArrayList;

 

public class hello {

    public static void main(String[] args) {

        // create an empty array list

        ArrayList<String> color_list = new ArrayList<String>();

 

        // use add() method to add values in the list

        color_list.add("White");

        color_list.add("Black");

        color_list.add("Red");

        System.out.println("List of the colors :" + color_list);

 

        //Return portion of the list : fromindex(inclusive)->1,  toindex(exclusive)->3

        ArrayList<String> new_color_list1 = new ArrayList<String>(color_list.subList(1, 3));

        System.out.println("Portion of the list: " + new_color_list1);

    }

}

 

 

官方文档如下: 

public List<E> subList(int fromIndex,

                       int toIndex)

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations.

This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:

 

      list.subList(from, to).clear();

 

Similar idioms may be constructed for indexOf(Object) and lastIndexOf(Object), and all of the algorithms in the Collections class can be applied to a subList.

The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

 

Specified by:

    subList in interface List<E>

Overrides:

    subList in class AbstractList<E>

Parameters:

    fromIndex - low endpoint (inclusive) of the subList

    toIndex - high endpoint (exclusive) of the subList

Returns:

    a view of the specified range within this list

Throws:

    IndexOutOfBoundsException - if an endpoint index value is out of range (fromIndex < 0 || toIndex > size)

    IllegalArgumentException - if the endpoint indices are out of order (fromIndex > toIndex)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540186.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[转载] Java中Array(数组)转List(集合类)的几种方法

参考链接&#xff1a; Java中的数组类Array 1、循环。新建List类&#xff0c;循环填充。 2、利用Arrays类的静态方法asList()。 Arrays.asList(T[])返回Arrays类的一个内部内List(T)&#xff0c;此类继承自AbstractList&#xff0c;不可增删。若想要一个可以增删的List类&am…

Linux查看系统cpu个数、核心书、线程数

Linux查看系统cpu个数、核心书、线程数 现在cpu核心数、线程数越来越高&#xff0c;本文将带你了解如何确定一台服务器有多少个cpu、每个cpu有几个核心、每个核心有几个线程。 查看物理cpu个数 cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l 查看核…

[转载] java中数组的反射的探究

参考链接&#xff1a; Java中的反射数组类reflect.Array 数组的反射有什么用呢&#xff1f;何时需要使用数组的反射呢&#xff1f;先来看下下面的代码&#xff1a; Integer[] nums {1, 2, 3, 4}; Object[] objs nums; //这里能自动的将Integer[]转成Object[] Object obj n…

防火墙iptables之常用脚本

防火墙iptables之常用脚本 转自&#xff1a;http://zhujiangtao.blog.51cto.com/6387416/1286490 标签&#xff1a;防火墙 主机 1。不允许别人ping我的主机&#xff0c;但是我可以ping别人的主机 #!/bin/bash iptables -F iptables -X iptables -Z modprobe ip_tables modprobe…

[转载] java中50个关键字以及各自用法大全

参考链接&#xff1a; Java中的默认数组值 关键字和保留字的区别 正确识别java语言的关键字&#xff08;keyword&#xff09;和保留字&#xff08;reserved word&#xff09;是十分重要的。Java的关键字对java的编译器有特殊的意义&#xff0c;他们用来表示一种数据类型&…

NFS 共享存储

服务器客户端yum -y install rpcbind nfs-utils 服务器 vim /etc/exports /data 192.168.10.0/24(rw,sync,no_root_squash) * ro # 只读权限 * rw # 读写权限 * sync # 同步&#xff0c;数据更安全&#xff0c;速度慢 * async #异步&#xff0c;速度快&#xff0c;效率高&a…

[转载] Java中的final变量、final方法和final类

参考链接&#xff1a; Java中的final数组 &#xff5c; Final arrays 1、final变量 final关键字可用于变量声明&#xff0c;一旦该变量被设定&#xff0c;就不可以再改变该变量的值。通常&#xff0c;由final定义的变量为常量。例如&#xff0c;在类中定义PI值&#xff0c;可…

Linux基础篇_01_计算机概论

学习资料&#xff1a;《鸟哥的Linux私房菜&#xff08;基础篇&#xff09;》部分&#xff1a;Linux的规划与安装 时间&#xff1a;20130225 学习笔记&#xff1a;计算机定义&#xff1a;接受使用者输入指令与数据&#xff0c; 经由中央处理器的数学与逻辑单元运算处理后&#x…

[转载] java中的经典问题:传值与传引用

参考链接&#xff1a; 有关Java中数组分配的有趣事实 参数传递的秘密 知道方法参数如何传递吗&#xff1f; 记得刚开始学编程那会儿&#xff0c;老师教导&#xff0c;所谓参数&#xff0c;有形式参数和实际参数之分&#xff0c;参数列表中写的那些东西都叫形式参数&#x…

[3/21]Windows Server 2008时钟方面的改进展示

在Windows Server 2008中的时钟显示和以往Windows Server 2003及以前的版本显示有很大的差别。如果要显示并进行简单的时间修改可以在时钟上双击&#xff0c;会出现如下图所示的界面。在上图中可以调整但无法进行真正的修改&#xff0c;彻底修改需要点击&#xff02;更改日期和…

[转载] 黑马程序员_学习笔记8_C#基础归纳之数组

参考链接&#xff1a; Java中的锯齿数组Jagged array ---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流&#xff01; ---------------------- 什么是数组&#xff1f; 数组是一组数据结构&#xff0c;它可以包含同一类型的多个元素。C#用特殊记号还…

2Python全栈之路系列之MysQl基本数据类型

Python全栈之路系列之MySQL基本数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的。 MySQL支持多种类型&#xff0c;大致可以分为三类&#xff1a; 数字类型 日期和时间类型 字符串类型 数字类型 类型大小用途BIT-二进制TINYINT1字节小整数值INT or INTEGER4字…

[转载] JAVA笔记_(Day04,Day05)函数数组

参考链接&#xff1a; 了解Java中的数组IndexOutofbounds异常 文章目录 函数定义练习误区重载&#xff08;overload&#xff09;重载选择题练习函数的内存调用问题 数组定义数组的内存图解数组的常见问题应用求和最大值将数组转成字符串查表法转十六进制查表版&#xff08;十六…

VDI序曲二 RemotoAPP部署

首先&#xff0c;我们需要准备如下角色&#xff1a;沿用VDI序曲一的2台物理服务器以及角色我们在物理服务器1的hyper-v上&#xff0c;我们利用之前我介绍的“服务器虚拟化之准备母盘VHD”的方法再创建如下虚拟机&#xff1a;WIN-RDAPP&#xff1b;WIN-RDWA&#xff1b;WIN-RDCB…

[转载] Java ArrayList toArray(T[] a) 解惑

参考链接&#xff1a; Java中的Array vs ArrayList 先看一个小的代码片段 ArrayList<Integer> arrayList new ArrayList<>(); Collections.addAll(arrayList, 11, 21, 31, 41, 51); Integer[] a new Integer[0]; Integer[] b new Integer[arrayList.size()]; …

CentOS7使用firewalld打开关闭防火墙与端口(转载)

1、firewalld的基本使用 启动&#xff1a; systemctl start firewalld 查看状态&#xff1a; systemctl status firewalld 停止&#xff1a; systemctl disable firewalld 禁用&#xff1a; systemctl stop firewalld 2.systemctl是CentOS7的服务管理工具中主要的工具&#xff…

多任务管理类 MutilTaskManager

计算和计算所需的数据能被较为平均的非配到若干task的时候&#xff0c;下面的任务管理类可以提供在大数据大计算量的情况下非精确的控制task的执行数量来限制计算量和内存占用量.下面是代码&#xff08;非线程安全版本&#xff09;&#xff1a; public class MutilTaskManager{…

[转载] Scanner和bufferreader读取控制台字符的区别

参考链接&#xff1a; Java中Scanner和BufferReader类之间的区别 从开始学习Java就用了scanner&#xff0c;因为比较简单每当遇到空格键或者换行键则读取下一个字符&#xff0c;一般用法 while(input.hasNextInt()){ int n input.nextInt(); int t input.nextInt(); int c …

Node.js meitulu图片批量下载爬虫1.051

原有1.05版程序没有断点续传模式&#xff0c;现在在最近程序基础上改写一版1.051. // // meitulu图片批量下载爬虫1.051 // 用最近的断点续传框架改写原有1.05版程序 // 2017年11月21日 //// 内置https模块 var httpsrequire("https");// 内置http模块 var httprequi…