Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6838829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:38:39+00:00 2026-05-26T23:38:39+00:00

Say I have an array of integers, int[] array = new int[7]; for(int i

  • 0

Say I have an array of integers,

int[] array = new int[7];
for(int i = 0; i < 7; i++)
{
array[i] = i;
}

Now i want to get only the first four numbers in that array, and turn put that into another array.

So I really want something like…

newArray = array[0-3].

I know that syntax is wrong, but I’m just giving the general idea of what I’m trying to do, is anything like that possible? Or do i have to create a loop and add it manually into the newArray?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:38:40+00:00Added an answer on May 26, 2026 at 11:38 pm

    Method 1

    int[] newArr = new int[4];
    System.arraycopy(array, 0, newArr, 0, 4);
    

    The method takes five arguments:

    1. src: The source array.
    2. srcPosition: The position in the source from where you wish to begin
      copying.
    3. des: The destination array.
    4. desPosition: The position in the destination array to where the copy
      should start.
    5. length: The number of elements to be copied.

    This method throws a NullPointerException if either of src or des are null.
    It also throws an ArrayStoreException in the following cases:

    • If the src is not an array.
    • If the des is not an array.
    • If src and des are arrays of different data types.

    Method 2

    Utilize

    Arrays.copyOf(array,4) to copy the first 4 elements, truncating the rest.

    of

    Arrays.copyOfRange(array,1,5) to copy elements 1-4 if you need the middle of an array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a dynamic array like: int* integers = new int[100]; Is there
I have an array of integers say int[] vals . I want to use
Say I have the following array of integers: int[] numbers = { 1, 6,
Say I have an array of table DDL queries and I want to get
I want to put some numbers from Excel to array. Let's say I have
Say you have an array of positive integers, manipulate them so that the concatenation
I have a array of integer numbers (say 1,2,3,4,5 or 1,2,3, ... 10 or
Let's say I have array of bytes: byte[] arr = new byte[] { 0,
Say I have an array that represents a set of points: x = [2,
Say I have an array of values: string[] text = new string[] { val1,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.