1

I am building an array based off comparing two other arrays. But when I initalize my third array I have to set the length. But that is making my array have null objects in some instances. Is there away I can drop the empty/null postions in the array. See my code so far below:

  private String[]  tags     = new String[] { "Mike", "Bob", "Tom", "Greg" };
  private boolean[] selected = new boolean[tags.length];
  public String[] selected_tags = new String[tags.length];

        for (int i = 0; i < tags.length; i++) {
            if (selected[i] == true){
               selected_tags[i] = tags[i];
            }
        }

I left out the code for the checkboxes that builds the Boolen selected [].

Either way if I only select 2 tags then my selected_tags[] array will be Mike, Bob, Null, Null

I need to get the Null Null out. Thanks in advance!

1
  • I suggest you use an ArrayList instead. Commented Jul 24, 2011 at 6:58

4 Answers 4

9

You can use ArrayList, instead of array.

private String[]  tags     = new String[] { "Mike", "Bob", "Tom", "Greg" };
private boolean[] selected = new boolean[tags.length];
public List<String> selected_tags = new ArrayList<String>();

for (int i = 0; i < tags.length; i++) {
    if (selected[i] == true){
        selected_tags.add(tags[i]);
    } 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can believe I just wasted an hour trying to hack it together when this was that easy. Thanks!
@Denoteone Please click on the check mark next to the answer that answers your question. That way others will know right away that the question is answered. (It's also nice to up-vote all helpful answers.)
@Ted Hop I had to test my code based on the helpful feedback. I always up vote and accept answers. I apologize but it was 3am and I simply feel asleep.
4

No, you can't drop the null values (and change the length of the array) after you've created it. You'll have to create a new one (or for instance use an ArrayList as illustrated below):

List<String> list = new ArrayList<String>();

for (int i = 0; i < tags.length; i++)
    if (selected[i] == true)
        list.add(tags[i]);

// Convert it to an array if needed:
selected_tags = list.toArray(new String[list.size()]);

Comments

2

As others have mentioned, this is much easier with an ArrayList. You can even get a regular array from it with the toArray function.

Without using ArrayList, you would have to figure out the length first and not include the null values. As you can see, that's a little messy:

int length = 0;
for( boolean b : selected ) if(b) ++length; // Count the "true"s

String[] selected_tags = new String[length];
for( int i = 0, j = 0; i < tags.length; i++ )
    if( selected[i] )
        selected_tags[j++] = tags[i];

Comments

2

Instead of using a standard Java array, you should use an ArrayList : it'll allow you to add elements to it, automatically growing the list as needed.


Basically, you'd first declare / instanciate the ArrayList, without specifying any kind of size :

public ArrayList<String> selected_tags = new ArrayList<String>();

And, then, in your loop, you'd use the add() method to add items to that ArrayList :

selected_tags.add(tags[i]);

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.