-3

I'm given an array of int and I need to return a new array with the number 3 added to the start of the array.

So if the array is MyArray[1,2,3] I need to create new array that says [3,1,2,3].

I'm not sure how I am supposed to do this without using ArrayList. I need to do it with just loops.

Iqbal hossain
  • 1,778
  • 3
  • 17
  • 24
briann
  • 143
  • 7
  • 3
    Java arrays are of a fixed length, hence we normally use ArrayList for this sort of thing, Are you allowed to create a new array? – dave Sep 05 '17 at 04:53
  • Use two arrays if its a must, one with a size+1 to another and add the elements as your problem statement states. – Naman Sep 05 '17 at 04:54
  • Yea. I'm supposed to create a new array that copies over the elements from the existing array. So in the post the existing array would be MyArray and I need to make a new array that copies what MyArray has, but with just a 3 in the front. – briann Sep 05 '17 at 04:55

3 Answers3

3

Try something like this:

public int[] insert(int[] src, int value)
{
    int[] dest = new int[src.length + 1];
    dest[0] = value;
    for (int i=0; i<src.length; i++)
    {
        dest[i+1] = src[i];
    }
    return dest;
}

You can use it like this:

int[] newArray = insert(MyArray, 3);

This is the basic idea. You'll need to put this into a class, add error checking (eg. what happens if src is null), etc.

dave
  • 11,641
  • 5
  • 47
  • 65
  • Hey this is awesome, thank you! So the only problem is I'm confined to what I can do since I'm doing this on a practice website (I tried it in eclipse and it works perfect). The method i'm supposed to add it into starts with this `int[] prepend5(int[] nums) ` and I can't change it since it's a practice problem. Is there a way to add the 3 to the begging still? – briann Sep 05 '17 at 05:06
  • The general idea should be transportable to your practice website. Feel free to mark this as the correct answer :) – dave Sep 05 '17 at 05:08
  • yeah it sort of works. I need to use this code in a method `int[] prepend5(int[] nums)` Without the insert it only returns 0 at the start of the array. – briann Sep 05 '17 at 05:12
  • Take the body of `insert()` and use it within `prepend5()`. Instead of `value`, simply use a hard-coded `3`. Use `nums` instead of `src`. – dave Sep 05 '17 at 05:17
  • @dave I'm dumb. I didn't realize I could just hardcode the 3 instead of Value. This makes a lot of sense now. Thank you. – briann Sep 05 '17 at 05:22
  • No problem. Perhaps I shouldn't have generalised my answer to accept any value (as opposed to hard-coding a 3). – dave Sep 05 '17 at 05:34
1

There is no need for an explicit loop here, because you can presumably use System.arraycopy(Object src, int srcPost, Object dest, int destPos, int length). First, decide how you want to handle null input for your array (returning a new one element array is what I would expect). Otherwise, create a new array with room for one more element. Set the first value, then copy everything at an offset of 1. Finally, return the new array. Like,

public static int[] insertValue(int[] src, int value) {
    if (src == null) {
        return new int[] { value };
    }
    int[] dest = new int[src.length + 1];
    dest[0] = value;
    System.arraycopy(src, 0, dest, 1, src.length);
    return dest;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • *First, decide how you want to handle null input for your array (returning a new one element array is what I would expect).* I'd much sooner let it throw a NPE. It's the most natural and logical course of action IMO. – shmosel Sep 05 '17 at 05:07
  • @shmosel Then you could simply not use the `if` (different strokes for different folks). – Elliott Frisch Sep 05 '17 at 05:10
0

You can use System.arrayCopy()

public int[] insert(int[] src, int value) {
        int[] dest = new int[src.length + 1];
        dest[0] = value;
        System.arraycopy(src, 0, dest, 1, src.length);
        return dest;
    }
Vega
  • 27,856
  • 27
  • 95
  • 103
Gajendra Naidu
  • 395
  • 3
  • 16