HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Array / Java Copy Array Range Example

Java Copy Array Range Example

Java example to create subarray from array i.e. creating array slice. Learn to use Java 8 Arrays.copyOfRange() method along with converting the subarray to list object.

Quick Reference:

String[] names = {"Alex", "Brian", "Charles", "David"};

//Subarray from index '0' (inclusive) to index '2' (exclusive)
String[] partialNames = Arrays.copyOfRange(names, 0, 2);	
// [Alex, Brian]

//Subarray to list
List<String> namesList = Arrays.asList( Arrays.copyOfRange(names, 2, names.length) );	
// [Charles, David]

1. Arrays.copyOfRange()

Use this method to copy the specified range of the specified array into a new array. It takes 3 parameters – original array, initial index and final index to be copied.

/**
* @param <T> the class of the objects in the array
* @param original the array from which a range is to be copied
* @param from the initial index of the range to be copied, inclusive
* @param to the final index of the range to be copied, exclusive.
*/
public static <T> T[] copyOfRange(T[] original, int from, int to) {
    return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());
}
'to' index parameter can be greater than index length. No ArrayIndexOutOfBoundsException will be raised in this case.

1.1. Array range copy example

String[] names = {"Alex", "Brian", "Charles", "David"};			// [Alex, Brian]

//Copy till second name from with index '0'
String[] partialNames = Arrays.copyOfRange(names, 0, 2);		// [Charles, David]

//Copy all names from with index '2'
String[] endNames = Arrays.copyOfRange(names, 2, names.length);	// [Charles, David, null, null, null, null, null, null]

//Copy last 8 names start with index '2'
//No ArrayIndexOutOfBoundsException error
String[] moreNames = Arrays.copyOfRange(names, 2, 10);	

2. Subarray to Arraylist

Java example to create arraylist from subarray. It is done in two steps:

  1. Create subarray from array with desired items.
  2. Convert array to list.
String[] names = {"Alex", "Brian", "Charles", "David"};

//Array to sublist
List<String> namesList = Arrays.asList( Arrays.copyOfRange(names, 0, 2) );	// [Alex, Brian]

Drop me your questions in comments section.

Happy Learning !!

References:

Arrays.copyOfRange()
Arrays.asList()

Sourcecode Download

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Array

  • Java – Array Introduction
  • Java – Print Array
  • Java – Print 2D Array
  • Java – Copy Array
  • Java – Copy Array Range
  • Java – Clone Array
  • Java – Array Deep Copy
  • Java – String to String[]
  • Java – byte[] to String
  • Java – String to byte[]
  • Java – Array Union
  • Java – Array Intersection
  • Array – Remove duplicate elements

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)