参考链接: 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)

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

  1. Java:获取数组中的子数组的多种方法

    Java:从一个数组中创建子数组 使用Arrays.copyOfRange函数 Arrays.copyOfRange支持:boolean[], byte[] ,char[],double[],floa ...

  2. 结对开发项目:求整数数组中连续子数组和的最大值

    小组成员:安娜 王鑫楠 题目二:求整数数组中连续子数组和的最大值. 1.分析过程: 题目收到后,前五分钟设计算法,首先也想到是不是一次遍历可以解决,考虑了一下觉得难度有点大最后还是果断放弃.我刚开始的 ...

  3. java删除数组中重复元素的几种方法

    Java操控数组,删除数组中所有的重复元素,使其数组元素全部唯一,有以下几种方法: 1,使用set(最好想到),set本身就是不重复的集合: package Array_test;import jav ...

  4. 数组中求子数组和最大

    要求数组中子数组和的最大值用了两次循环的办法, 方案1:块内先比较大小,一块一块的进行,需要嵌套循环 方案2:两个数的子数组先比较,然后三个数的,四个数的.....再比较,可以不嵌套循环但感觉不够方便 ...

  5. php数组遍历相同的元素覆盖_php获取数组中重复数据的两种方法

    搜索热词 代码如下: PHP function FetchRepeatMemberInArray($array) { // 获取去掉重复数据的数组 $unique_arr = array_unique ...

  6. php 获取数组最小值,php 获取数组中最小的值与键名的方法

    上一篇博文说的是php获取数组中最大的值与键名的方法,那么这篇博文就说一下php获取数组中最小的值与键名的方法.获取数组中最小的值可以使用php中的预设函数 min() ,其使用方法也非常的简单. p ...

  7. java数组删除元素_java删除数组中的某一个元素的方法

    下面小编就为大家带来一篇java删除数组中的某一个元素的方法.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧 实例如下: package org.company.proj ...

  8. php 从数组里删除元素,PHP从数组中删除元素的四种方法实例

    PHP从数组中删除元素的四种方法实例 一.总结 一句话总结:unset(),array_splice(),array_diff(),array_diff_key() 二.PHP从数组中删除元素的四种方 ...

  9. python获取数组中大于某一阈值的那些索引值_Python NumPy 高级索引——整数组索引、布尔索引及花式索引...

    NumPy 除了之前文章中介绍的用整数和切片的索引外,数组还可以由整数数组索引.布尔索引及花式索引. 整数数组索引 整数索引有助于基于 N 维索引来获取数组中任意元素.每个整数数组表示该维度的下标值. ...

最新文章

  1. php layui table,layui table 相关问题汇总
  2. 设置element表格透明样式
  3. 为什么relativelayout.layoutParams的width为-1
  4. 记录我对Padding Oracle攻击的分析和思考之抄写
  5. 搜索的php mysql代码生成器_四款强大的PHP代码生成器
  6. TensorFlow HOWTO 1.3 逻辑回归
  7. DocumentFragment文档片段示例
  8. [Liferay] Liferay 实现单点登录 - OpenLDAP
  9. android学习1:初识Activity
  10. OpenCV adaptiveThreshold 自适应阈值
  11. ITIL好看不好吃?(二)
  12. mysql 统计每天、每周、每月、每年数据
  13. 用java编写注册界面_java编写一个注册界面的方法
  14. java替换字符串_java string中的替换字符串
  15. Flink:No operators defined in streaming topology. Cannot execute.
  16. Windows API 关于控件的自绘——文本颜色、背景颜色、字体
  17. 如何在阿里云(centos7)上面搭建fastdfs服务器(搭建篇)--保姆级超级详细
  18. 【章节总结】理科数学——立体几何
  19. ​在 2022 年找工作,毕业生们的 “最后一课”
  20. 该如何选择IT培训机构?

热门文章

  1. 目标检测第5步:如何在Windows 10系统下,搭建YOLOv5(5.0)环境?保姆级,没有人比这个更详细了(更新时间2022.3.22)
  2. (三)linux之根文件系统的制作
  3. php页面的循环输出数组,PHP抓取页面上的数组 并循环输出 急
  4. python 笔试题 英方_4000字转型数据分析师笔试面试经验分享
  5. 鸿蒙系统替代iOS,华为横空出世!鸿蒙系统,能否替代安卓IOS?
  6. python批量跑plsql_python实现自动化报表(Oracle/plsql/Excel/多线程)
  7. cudnn.deterministic = True 固定随机种子
  8. JDK源码解析之 Java.lang.Package
  9. Oracle GoldenGate经典架构
  10. sed教程入门与实例练习(二)