Adding data to arrays Java

问题内容:

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;
  }
}

问题评论:

2  
Take an ArrayList instead
1  
ArrayList works the same way
    
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.
    
consider you should change second loop condition to list.length.

答案:

答案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()));
}

答案评论:

    
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.
– Peregrine Lennert
29 mins ago
    
Completely understandable @PeregrineLennert hope I helped

答案2:

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.

答案评论:

    
Thank you for recommending ArrayLists, the only thing is I was trying to avoid using ArrayLists, as I am a beginner at Java.
– Peregrine Lennert
3 hours ago
    
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.

原文地址:

https://stackoverflow.com/questions/47753043/adding-data-to-arrays-java

Add a Comment