2

In my program, I am trying to add values to an array while it is looping. Before it is put in the array, it has to fulfill the if statement. I need to be able to add as many values as needed based on the input. I am not sure how to do this. Any help would be appreciated.

for(int a=0; a<= subset1white.length-1;a++){
  String w = Integer.toString(Integer.parseInt(subset1white[a]) + 2);
  String x = Integer.toString(Integer.parseInt(subset1white[a]) - 2);
  String y = Integer.toString(Integer.parseInt(subset1white[a]) + 10);
  String z = Integer.toString(Integer.parseInt(subset1white[a]) - 10);
  String[] arithmetic = {w, x, y, z};
     for(int b=0; b<= arithmetic.length-1; b++){
        if(arithmetic[b] == subset1black[a]){

        }
     }

  }

If the if loop returns true, I need the subset1black[a] value to be put in an array called result. I know how to declare arrays but I do not know how to declare an array where the length can be changed(new values added).

1
  • 1
    Why don't you use the ArrayList? It's like an array that can grow dynamically when needed. Commented Mar 16, 2014 at 1:52

2 Answers 2

3

Use ArrayList<Integer> which can grow dynamically as needed (as suggested by Bhesh Gurung):

// Declare
List<Integer> result = new ArrayList<Integer>();

// Add to end of the list
result.add(subset1black[a]);

See the Javadoc for more information or if you want to add the elements to the list in a different way.

If you really need a primitive array, you can convert the List. See this related Q.

Sign up to request clarification or add additional context in comments.

Comments

0

There are no variable size primitive arrays in Java, so the closest to such a data structure is the ArrayList class, which internally is an array of objects, resized when necessary. It helps to initialize the ArrayList to the anticipated size (or a bit more), to avoid resizing, but this optimization is not necessary.

For the code in question, an implementation of the above could be:

List<Integer> result = new ArrayList<Integer>(subset1white.length);

for (int a=0; a<subset1white.length; a++) {

  String w = Integer.toString(Integer.parseInt(subset1white[a]) + 2);
  String x = Integer.toString(Integer.parseInt(subset1white[a]) - 2);
  String y = Integer.toString(Integer.parseInt(subset1white[a]) + 10);
  String z = Integer.toString(Integer.parseInt(subset1white[a]) - 10);

  String[] arithmetic = {w, x, y, z};

  for (int b=0; b<= arithmetic.length-1; b++) {
    if (arithmetic[b] == subset1black[a]) {
      result.add(new Integer(subset1black[a]));
    }
  }    
}

It is not necessary to initialize an Integer object to add to the ArrayList, since Java does that automatically (via "autoboxing"), but doing so makes the code a bit cleaner.

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.