0

let's assume that we have an array list of size 3, of some object type (say class named Patient that has attributes of it's own).

so we know about the is Empty() method that checks if the entire given array list is empty or no, but is there a way to loop around the array list and check at which index there is object or not.

3
  • to be more clear , Array List<Patient> patient List = new Array List<>(3); patient List .add(patient1); patient List .add(patient2); sorry for the spaces so at index 2 there is no objects but at 0,1 there is Commented Dec 13, 2021 at 19:52
  • What part of of "loop around the array list and check at which index there is object or not" are you not sure can be done? Commented Dec 13, 2021 at 19:53
  • @ScottHunter whether i can detect the indices where the arrayList is empty or no Commented Dec 13, 2021 at 20:06

1 Answer 1

2

You can do it like this.

  • stream the indices of the list
  • filter those locations that are null
  • put them in an array
List<String> list = Arrays.asList("a",null,"b","c","d",null,"e");

int[] nullLocations = IntStream.range(0,list.size())
        .filter(i->list.get(i) == null)
        .toArray();

System.out.println(Arrays.toString(nullLocations));

prints

[1, 5]

Here is a non-streams version using the same list as above

List<Integer> nullLocations = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
     if (list.get(i) == null) {
         nullLocations.add(i);
     }
}
System.out.println(nullLocations);

prints

[1, 5]      
Sign up to request clarification or add additional context in comments.

5 Comments

thank you so much for the effort!! but i guess this is some advanced solution (at least for me right now when i'm still in the mid of the course)
@RoronoaNewJavaBoy I added a non-streams version using a simple loop.
perfect, you made it sound so simple thank you.
@RoronoaNewJavaBoy You misunderstand the purpose of the argument to ArrayList. It is the capacity, not the number of elements. An array list is a wrapper for an array that grows dynamically. If there is no more room in the backing array, then it needs to be copied with a new length. When adding lots of values, this can be inefficient due to lots of copying. So you have an option to specify the initial capacity to reduce that activity. I believe the default is 16 but I am not certain. To find the number of elements, use the size() method.
i just went back to read my notes, yeah i mistaken the size and the capacity but now i'm wondering about how i can reach or get that capacity , since the arraylist will continue to get adds from the user, but i wanna put a limit to it based on it's capacity. i know in this case the use or arrays is more likely effictient than arraylist but you know how exams be like

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.