Creating sub-array

Posted on 2008-11-16
Last Modified: 2013-11-23
Is it possible to create a sub-array from an array directly without having to copy the elements?

Let's say that I have an array of length 1000, and I want to create an array from index 0 to index 400.

Without having to copy each element into a new array, can I do something like int [] newArray = oldArray(startIndex, endIndex)?

Question by:ubuntuguy
    LVL 86

    Expert Comment

    No - you need to copy it
    LVL 86

    Accepted Solution

    //I want to create an array from index 0 to index 400

    int[] newArray = new int[401];
    System.arrayCopy(oldArray, 0, newArray, 0, newArray.length);

    Open in new window


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    linearIn  challenge 23 55
    Java virtual machine monitoring tools 3 30
    noX challenge 17 55
    How Complex Is This Java Course ? 9 52
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now