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
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);
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 :-)
Related
Why is this ArrayList empty when I print it out?
ArrayList<InterviewQuestion> interviewQuestionArrayList = new ArrayList<>();
for (int i = 0; i <interviewQuestionArrayList.size(); i++) {
interviewQuestionArrayList.add(new InterviewQuestion());
}
System.out.println(interviewQuestionArrayList);
You have created arraylist but before adding you are trying to looping through it.. size will be zero initially..
If you know how many interviewQuestions are there then you can loop with that number.
ex:
for (int i = 0; i <numberOfQuestions; i++) {
interviewQuestionArrayList.add(new InterviewQuestion());
}
numberOfQuestions should be predifined..
Initially if you dont know how many times you want to add then you can use while loop. But you should exit from while based on some condition
like,
boolean flag = true;
while(flag) {
interviewQuestionArrayList.add(new InterviewQuestion());
if(condition) flag = false;
} //something like this
As far as i can see, your List is empty when you enter the loop.
Thus its size() will return 0 and the loop will not add any elements.
your code basically does
for (int i = 0; i < 0; i++) {
interviewQuestionArrayList.add(new InterviewQuestion());
}
Here you create an empty list -> this means the size is currently 0
ArrayList<InterviewQuestion> interviewQuestionArrayList = new ArrayList<>();
The following loop is never executed because i (now 0) is never < size (now 0)
This means there are no Objects added to your list
for (int i = 0; i <interviewQuestionArrayList.size(); i++) {
interviewQuestionArrayList.add(new InterviewQuestion());
}
Try using i<10 as the condition in the for loop to add 10 elements to the list
if this 2 staments are followed by each other, then your list is never populated:
ArrayList<InterviewQuestion> interviewQuestionArrayList = new ArrayList<>();
for (int i = 0; i <interviewQuestionArrayList.size(); i++) {
interviewQuestionArrayList.add(new InterviewQuestion());
}
ergo, you have an empty list to print
System.out.println(interviewQuestionArrayList);
you can play with some dummyData that you add when declaring the list
I've got something like this:
List<int[]> myList= new ArrayList<int[]>();
int tmp[] = new int[size];
for(int i = 0; i<anotherList.size(); i++) {
for (int j = 0; j<size; j++) {
tmp[j] = anotherList.get(i)[j];
}
myList.add(tmp);
}
When I want to print myList, I see only last added tmp. Why?
for(int i = 0 ; i<myList.size(); i++){
for(int j=0; j<myList.get(i).length; j++){
System.out.print(myList.get(i)[j]+ " ");
}
System.out.println();
}
Your code only ever creates one array named "tmp". You need to move the declaration of tmp inside the first for loop, instead of having it before the for loop.
The tmp array is constructed outside the loops, and thus the same instance is being added to myList. To fix, add a different instance to the List at every iteration (eg create the array inside the first for loop)
void radix(int[] arr, int maxDigit){
int exp = 1;
for (int i=0; i<maxDigit; i++) {
ArrayList[] bucket = new ArrayList[10];
for (int n=0; n<10; n++){
bucket[n] = new ArrayList<Integer>();
}
for (int j=0; j<arr.length; j++){
bucket[(arr[j]%exp)%10].add(arr[j]);
}
int ind=0;
for (int k=0; k<10; k++){
for (int n : bucket[k]){
arr[ind] = n;
ind++;
}
}
exp*=10;
}
}
above is my attempt for a radix sort in java.
I keep having an error at this for loop:
for (int n : bucket[k])
Saying that Object can't be converted to int.
However, I declared the array to have an arrayList as its element,
so bucket[k] should be an arraylist.
Can anyone please explain why this happens and how I could get around this problem?
I would appreciate any help. Thanks
You have an ArrayList<Integer>. Try using for(Integer n : bucket[k])
You also need to provide a type for ArrayList. Try ArrayList<Integer>[] bucket = new ArrayList[10];
ArrayList[] bucket = new ArrayList[10];
This does not have the type parameter.
And you should avoid using inflexible, low-level constructs like arrays anyways.
Try to actually express your intents in your code with identifiers and to use first-class objects for all of your key concepts.
You have an ArrayList not ArrayList.
Change:
for(int n : bucket[k])
To:
for(Integer n : bucket[k])
You also need to type your ArrayList declaration with:
ArrayList<Integer>[] bucket = new ArrayList<Integer>[];
public class Test {
private LinkedList<object>[] array = new LinkedList[10];
for(int i = 0; i < 0; i++){
array[i] = new LinkedList<hashNode>();
}
}
This is the code that I have which is far off from right, I know that I need to initialize a new LinkedList for each position in the array but I can't figure out how to do that within the class. I basically want an array of ten linked lists to be the variable of my object and I'm stumped on how to solve this.
You can do this in an initializer block:
public class Test {
...
private LinkedList<object>[] array = new LinkedList[10];
...
{
for(int i = 0; i < array.length; i++){
array[i] = new LinkedList<hashNode>();
}
}
...
}
Or in the constructor:
public class Test {
...
private LinkedList<object>[] array = new LinkedList[10];
...
public Test () {
for(int i = 0; i < array.length; i++){
array[i] = new LinkedList<hashNode>();
}
}
...
}
Note also that Java is case-sensitive, and unless you've defined something named object, you probably meant Object, or you can use a wildcard ?.
By the way, your loop condition was incorrect in your example, you meant i < array.length, not i < 0.
I have an arraylist populated by four elements, the order of which is random (they are put here by random from another arraylist). I then have a for loop that repeats 10 times, at the end of each repetition I use the clear methods to clear all the elements of the arraylist. However, when I start a new repetition, I would like to repopulate my arraylist with the old (previously worked with) elements that were members of the list in the previous repetition, so that I can use the elements again. And I would like to repeat that until I get out of my 10-repetition for loop. Is there any way to achieve this at all?
Code in addition to my question:
ArrayList<String> answerPegs = new ArrayList<String>();
// add element to ArrayList
ArrayList<String> mySecretAnswer = new ArrayList<String>();
for (int n = 4; n > 0; n--)
{
//populate mySecretAnswer with elements from answerPegs
}
ArrayList<String> clone1 = mySecretAnswer;
for (int q = 0; q < 10; q++) {
for (o = 0; o < 4; o++)
{
}
// called clear() method here
} // END OF 10-ROW LOOP
I would suggest simply having 2 lists - keep a pristine copy of the original list, and then iterate over + clear a copy of that list.
public void doRepetitions(List<Object> original)
{
for( int i=0; i<10; i++ )
{
List<Object> working = new ArrayList<Object>( original );
doStuffWithList(working);
}
}
Edit:
Since you've posted your code, I can give a more specific answer:
You can change your clone to be:
ArrayList<String> clone1 = new ArrayList<String>(mySecretAnswer);
And then move that to be inside your for loop:
for (int q = 0; q < 10; q++)
{
ArrayList<String> clone1 = new ArrayList<String>(mySecretAnswer);
// ....
}
Could you use 2 loops nested and just have the inner loop be the for loop 10 times then clear once at the end