0

I have an arraylist at the start of my class

private ArrayList<Dog> dogs;

This is how dog is declared in the dog class

public Dog(String name, ArrayList<Owner> owners, boolean likeBones, String food,
        int mealsPerDay);

I am trying to find the dogs who like bones and save that dog into result. The rest of the code works fine including .getLikesBones().

Dog[] result = null;
for (Dog dogSearch : dogs) {

        if (dogSearch.getLikesBones()){ 
            result =dogSearch; //I know this won't work, just simply showing what I want.
        }



    }
    return result;
}

thank you in advance for all help.

2
  • 1
    It didn't work is not a good explanation of your issue... Are you getting a nullpointerexception right? Think about it. Commented Apr 4, 2015 at 16:36
  • 2
    Sounds like a homework question. You should probably try and implement the stuff you studied so far. Commented Apr 4, 2015 at 16:37

5 Answers 5

2

As you may not know the result size. So, result should be declared as ArrayList instead of array. Look at below code.

List<Dog> result = new ArrayList<>();

Than add the search value into result.

for (Dog dogSearch : dogs) {
  if (dogSearch.getLikesBones()){ 
     result.add(dogSearch);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use a collection instead of using an array

List<Dog> result = new ArrayList<Dog>();
for (Dog dogSearch : dogs) {
        if (dogSearch.getLikesBones()){ 
             result.add(dogSearch);
        }
    }
    return result;
}

1 Comment

thank you this worked great. Just had to change the method to public List<Dog> and had to import java.util.List;
0

It would be better if you change Dog[] result to List<Dog> result = new ArrayList<>(), so you can add data without worrying on initializing the array at a fixed size.

Your code would look like this:

List<Dog> result = new ArrayList<>();
for (Dog dogSearch : dogs) {
    if (dogSearch.getLikesBones()){ 
        result.add(dogSearch);
    }
}
return result;

If you still need to return Dog[] rather than List<Dog>, then call List#toArray at the return statement:

return result.toArray(new Dog[result.size()]);

Comments

0
ArrayList<Dog> result = new ArrayList<Dog>();
for (Dog dogSearch : dogs) {

    if (dogSearch.getLikesBones()){ 
        result.put(dogSearch); 
    }

}
return result; }

Comments

0

you have 2 ways of doing this. you have declared a regular array of Dogs at the top of your code.

To do it with the regular array you will need to declare an int to hold the index of the array since there is no "add" function for regular arrays. So it would be :

Dog[] result = new Dog[dogs.lenght()];
int index = 0;
for (Dog dogSearch : dogs) {

        if (dogSearch.getLikesBones()){ 
            result[index] = dogSearch;
            index++;
        }
    }
    return result;
}

otherwise use an array list of dogs for the "result" and just do

arrayList.Add(dogSearch);

4 Comments

fails to allocate memory for result array.
how about initializing an array?
hmm.. I thought theres a way to declare array without specifying size... looking now
There is, it's called ArrayList :P If you insist on using an array for the result allocate one to be as large as it might need to be. Which in this case is dogs.length()

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.