2

I have 2 arrays of Member - one filled, the other not. I have a list which contains all members. I need to fill the second array with member who are not in the first array.

I tried this , but i got an outboundexception ( i am very bad with pure algorithmic :( )

Member[] busyMember= getBusyMember();
List<Team> allMembers= new ArrayList<Team>(jsonParsed.getAllElement());
Member[] availableMembers= new Member[allMembers.size()-busyMembers.length];


            for (int i= 0; i<allMembers.size(); i++) {
                Team t = allMembers.get(i);
                long memberId = t.getMember().getId();
                for (int j= 0 ; j<busyMember.length; j++) {
                    if (memberId != busyMember[j].getId()) {
                        availableMembers[i]=t.getMember();
                        //outboundexception here

                    }
                }

            }

Thank you very much for your help

2
  • 3
    Why arrays? Use sets and lists and you won't have these issues... Every second collections question seems to be on operating with arrays where standard collections would turn the problem into a trivial one. Sigh.... The way you ask your question cries for "do not use arrays" Commented Jan 9, 2014 at 19:19
  • @OlegS. I know it s much easier to treat with Collections instead arrays but in my case it's an instruction who i must respect :( Commented Jan 9, 2014 at 20:30

2 Answers 2

3

I would just use Lists and the removeAll method. If you really need an array, you can always call the method toArray(T[]) on the resulting list.

List<Member> busyMember= Arrays.asList(getBusyMember());
List<Team> allMembers= new ArrayList<Team>(jsonParsed.getAllElement());
List<Member> availableMembers = new ArrayList<Member>();
for(Team t : allMembers){
    availableMembers.add(t.getMember());
}
availableMembers.removeAll(busyMember);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help. Unfortunately I have to work with arrays because it is the instruction that was given to me. But i will opt for your solution and call toArray méthod
2

Assuming that allMembers has more elements than busyMember, you just need to change the statement

for (int i= 0; i<allMembers.size(); i++)

to

for (int i= 0; i<availableMembers.size(); i++)

There are other changes that you would need to do for consistency, but this is another discussion :-)

3 Comments

While this will resolve the current exception, this will result in another logical issue.
@PNS Thank you for your help . Indeed , this resolve my problem , but it doesn't read all datas of allMembers array( because it's limited to availableMembers.size())
Which means you need to do the revision, as per ZouZou's suggestion, or manually keep track of the correct index values.

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.