1

I have this ArrayList

[21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]

created this way

for (int i = 0; i < number; i++) {
        ArrayList.add(21+i);
}

In my code I have to remove, and sometimes return, some values from that without changing the position of the others. I tried with

ArrayList.add(removedValue, null);

so I had something like this

ArrayList = [21, 22, 23, null, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]

but in this way I cannot Sort the Array with Collections.

Is there something else that I can do to fix that, without destroying my array?

2
  • I think you don't communicate the problem very well. on one hand you want to sort the arrayList, but on the other hand you want to sort it. what exactly is the problem that you're trying to solve? Commented Jan 23, 2014 at 15:59
  • @AmirArad: This is a table of a board game. Players can pick cards from it and return cards into it. My problem is that when a player returns a card doesn't go into the right place, so I though of sorting the array. I created this ArrayList of Integers and maybe that's my main problem. Commented Jan 23, 2014 at 16:07

4 Answers 4

4

You should implement your own Comparator<Integer> and sort with it.

The code below uses a comparator which treats two Integer objects equal in the case either of them is null or both of them are null, thus resulting in no change of the indexes.

Integer[] numbers = new Integer[] { 21, null, 23, 24, 25, 26, 27, 28,
        29, 30, 31, 32, 33, 34, 35, 36 };

ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(numbers));

Collections.sort(list, new Comparator<Integer>() {
    @Override
    public int compare(Integer arg0, Integer arg1) {

        if (arg0 == null || arg1 == null)
            return 0;
        return arg0 - arg1;

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

6 Comments

this isn't gonna help me cause my list will change. Like I said I should not change the position of the other numbers. I could put a value of "-100" or something, if I could change the position in my Array.
@Akumu so you genius, can you tell me a way to treat 'null' sometimes as 24 sometimes as 34 etc. ?
Don't get mad my friend. I am just asking. If there was a way to sort the list and ignore null or something will be great.
@Akumu: Imagine you have [null, null, null, 4, null, null, null, 6, null]. How would you like it to be sorted? Consider using a LinkedHashMap instead with key value pairs. Perhaps this solves your problem better!
@bobbel: if I have that I wouldn't mind to sorted. But if I had to return the "5" in my array List I would like to have this '[null, null, null, 4, 5, null, null, 6, null]'.
|
0

You should look on Guava lib, there a nice explanation of why you shouldn't have any null on Collections.

https://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained

Some example of use.

public class OptionalInteger {
    final static Optional<Integer> NULL = Optional.absent();

    public static void main(String[] args) {

        final ArrayList<Optional<Integer>> list = Lists.newArrayList();

        for (int i = 21; i < 36; i++) {
            list.add(Optional.fromNullable(i));
        }

        list.add(10, NULL);
        list.add(12, NULL);

        System.out.println(list);

        Collections.sort(list, new Comparator<Optional<Integer>>() {

            @Override
            public int compare(Optional<Integer> o1, Optional<Integer> o2) {
                if(!o1.isPresent())
                    return 1;
                if(!o2.isPresent())
                    return -1;
                return o1.get().compareTo(o2.get());
            }
        });

        System.out.println(list);
    }
}

Comments

0

I advise you to re-consider your model.

you care about preserving order in the list because you want to use the list's index as a key - so why not use a Map or a SortedMap? you could use an Enum or some custom type as a key instead of integer.

1 Comment

I don't know how to use them, but I will look them up. Thanks for the advice.
0

Try with this...

Collections.sort(list, new Comparator<Integer>() {
        @Override
        public int compare(Integer arg0, Integer arg1) {

            if (arg0 != null && arg1 != null) {
                return arg0.compareTo(arg1);
            }
            if (arg0 == null) {
                return 1;
            }
            return -1;
        }
    });

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.