1

how can I check if the whole array of a type Class is empty? The way I did it doesn't seem to work for me, do I need to Iterate one by one to check if it is null? That would not support my purpose of the code though.

This is the deceleration of my array. private Item sysItem[] = new Item[10];

if (sysItem == null) {
  System.out.println("No item's to display right now");
  return;
}

My purpose of this above piece of code is, if the user didn't create an item, there won't be any items in the array so if the user was to display an Item from the array it should output the user saying there are no items in the array which is why I need to check if the whole array is empty or not.

Modi
  • 27
  • 5
  • 2
    `array.isEmpty()` - show your code instead of describing your array and state clearly what you are trying to achieve. – luk2302 Apr 14 '19 at 10:35
  • 3
    There is not enough code or background in this question. Please show the declaration of your array, what "your purpose of the code" is, how the array is supposed to implement that. All code snippets should be formatted with the `{}` button. – RealSkeptic Apr 14 '19 at 10:37
  • 1
    Please give me a second! – Modi Apr 14 '19 at 10:39
  • 1
    This `if (sysItem == null)` won't help, since you have initialized the array on creation, and so sysItem won't ever be null unless you explicitly assign it a null value or if you are shadowing the variable and not showing this to us. Why not iterate through the array with a for loop checking null on each *item* in the array? – Hovercraft Full Of Eels Apr 14 '19 at 10:44
  • Nope I'm not assigning it to null, what's the best approach to this? Initialise the array to Null or iterate through the whole array for null? – Modi Apr 14 '19 at 10:48
  • 2
    @luk2302 what's `array.isEmpty()`? – Andrew Tobilko Apr 14 '19 at 10:48
  • How are you planning to put and remove elements in this array? If elements should be placed from left to right and removal of element in the middle should shift elements after it to left to eliminate breaks all you need to do is check first element from the left (at index 0) since there all elements should start appearing (and if it has one element it isn't empty, if it doesn't nave element there, it can't have any element after it so it is empty). – Pshemo Apr 14 '19 at 10:49
  • @Pshemo, I will have to check the whole array, since during the middle of the program sometimes i remove the items if sold. So I will have to check if all elements and check if there is a non null value right? – Modi Apr 14 '19 at 10:52
  • 1
    This whole set up sounds like you want to use an `ArrayList` and not an array. Then you remove items the list shrinks, and all you need to do is to check that its `.size()` property is `> 0`:............... `if (myItemList.size() > 0) { ...}` – Hovercraft Full Of Eels Apr 14 '19 at 10:54
  • Other approach would be adding `counter` of elements which will be incremented when user added new element on empty space, and decreased if user removes some already existing element (when selected [index] holds Item and it is replaced with null). Then all you need is check if `counter==0` to see if it is empty, but like @HovercraftFullOfEels mentioned Lists already do that kind of tasks for us so I am not sure if there is any purpose in reinventing them. – Pshemo Apr 14 '19 at 11:00
  • @HovercraftFullOfEels A list would work, but this is a fixed array for 10. Cause there can be max amount of 10! – Modi Apr 14 '19 at 11:00
  • @Modi Then add stopping condition in method responsible for adding items like: `public void add(Item item){ if (list.size==10){ System.out.println("No more space in list"); } else { list.add(item); } }`. – Pshemo Apr 14 '19 at 11:04

0 Answers0