0

I had an idea for a way to bypass the hard limit on spaces for a normal array in Java. Would this work?

public class Array{
  private int[] group;

  public void addNums(int[] nums) {
    int[] list = new int[group.length + nums.length];
    for (int a = 0; a < group.length; a++)
      list[a] = group[a];
    for (int a = group.length; a < nums.length; a++)
      list[a] = nums[a - group.length];
    group = list;
  }
}
4
  • 2
    Take an ArrayList instead Commented Dec 11, 2017 at 12:36
  • 1
    ArrayList works the same way Commented Dec 11, 2017 at 12:36
  • Please follow the java Naming Conventions as it makes reading Java for others easier. e.g. a class should alwas start with a Capitalcased letter and a variable should always start with a lowercase letter. For the rest CamelCasing is used. Commented Dec 11, 2017 at 12:39
  • consider you should change second loop condition to list.length. Commented Dec 11, 2017 at 12:46

3 Answers 3

1

First of all, I commend you for trying to create your own ArrayList as you said you are a beginner to java.

You are really close there's just a small issue with your second for loop:

public void addNums(int[] nums) {
    int[] list = new int[group.length + nums.length];
    for (int a = 0; a < group.length; a++)
        list[a] = group[a];

    for (int a = group.length; a < list.length; a++)
        list[a] = nums[a -group.length];
    group = list;
}

You want it to loop to the end of your new listrather than nums because we want to use a to specify the position in the array the element is going.

Also, this might be a little offtopic but your solution there doesn't seem to be anywhere within your Array class to be able to specify what is within the group[]. So consider adding some getters and setters for testing:

public class Array {

    private int[] group;

    public int[] getGroup() {
        return group;
    }

    public void setGroup(int[] group) {

        this.group = group;
    }

And then when you are testing your addNums() method you could do something like this:

public static void main(String[] args) throws IOException {

    Array m = new Array();

    int[] startArray = {1,2,3,4,5};
    m.setGroup(startArray);

    int[] endArray = {6,7,8,9,10,11,12};
    m.addNums(endArray);

    System.out.println(Arrays.toString(m.getGroup()));
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the recommendations for using getter and setter methods, I do realize that there is no place that sets the group[], but for the sake of keeping the code short, I neglected to include how it gets set.
Completely understandable @PeregrineLennert hope I helped
You did indeed help!
0

Your approach will work, but you shouldn't re-invent the wheel. ArrayList. addAll() does this for you (one difference, there is no ArrayList<int>, only ArrayList<Integer>, but you'll hardly notice the difference).

So, I'd recommend to use ArrayList or Vector or some other List implementation.

3 Comments

Thank you for recommending ArrayLists, the only thing is I was trying to avoid using ArrayLists, as I am a beginner at Java.
Especially when you're a beginner at Java, you shouldn't avoid the standard Java solutions. So, your Array class might at best become an academic exercise, but for real-world programming, use ArrayLists.
Using Vector in 2017 is a very doubtful idea.
0

If you have to use int[] instead List<Integer>, you can use System.arraycopy() method.

public void addNums(int[] nums) {
    int[] newGroup = new int[this.group.length + nums.length];

    // copy elements from group array to newGroup array
    System.arraycopy(group, 0, newGroup, 0, group.length);

    // copy elements from nums array to newGroup array
    System.arraycopy(nums, 0, newGroup, this.group.length, nums.length);

    this.group = newGroup;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.