1

I have an array which is declared based on another array size and initially all the values are set to 0, although assigned dynamically the size is fixed(equal to other array size), as required in Java. next I have a for loop with certain if-else conditions. if a condition is true then add 1 to array first index similarly if next iteration is true then add 1 to second index and goes on. I want later the array to be Binary AND so if i get 1 it means all conditions are true, if it's 0 zero it means that some where the condition was false. I am not in favor of using ArrayList, also I am not sure if i can bound the length of ArrayList, as when i do and print it it shows the size as 0.

How can I dynamically add items to a Java array?

States what is my requirment but without using the Arraylist or List etc, as most of the answeres i found relevant are suggesting to use ArrayList.

Thanks

Community
  • 1
  • 1
Shahensha Khan
  • 265
  • 1
  • 19
  • 2
    If the array elements are only ever going to be `0` or `1`, why not just make it a `boolean[]` instead? Same thing, clearer intent. – JonK Aug 03 '16 at 08:02
  • yes thats a good suggestion, but how can i add the 1 in sequence like the add of arraylist do – Shahensha Khan Aug 03 '16 at 08:08
  • Are you trying to modify an existing element or add a new one? I think you might be getting confused here. ArrayLists are useful for *dynamically sized* collections, regular arrays are fine for *dynamic values* inside a fixed-length collection. – JonK Aug 03 '16 at 08:10
  • i am giving a try at @elyashiv answer. if not working then i will definately post the code for further discussion. thanks for your time and concern. i really appreciate people like you trying to give time – Shahensha Khan Aug 03 '16 at 08:16
  • @walen thanks for your help, i have already accepted the answer and it is working. thanks for your time. – Shahensha Khan Aug 03 '16 at 08:25

4 Answers4

3

Why not to do something like this:

for (int i = 0; i < arr.length; i++)
{
   if ( ... )
      arr[i] = 1;
   else ...
   if (...)
      arr[i] = 1;
}

in case of nested loops and more complicated logic you can do something like this:

int index = 0;
boolean flag = true;
while (still have what to do)
{
   for <complicated logic>
   if (flag)
   {
      arr[index] = 1;
      index++;
   }
   flag = true;
}

just make sure each condition is accumulated into flag like this - flag = flag && condition

notice you can short circuit the conditions - if you get one false, you can skip checking the rest of the conditions.

elyashiv
  • 3,623
  • 2
  • 29
  • 52
  • Actually there are four nested loops, and keeping track of the index relative to the for loop is not possible, also my array can be of length 3 and the inner loop can be more then 3(possible in my scenario). so thats y i am in need of some solution based on the scenario provided. – Shahensha Khan Aug 03 '16 at 08:02
2
for (i =0 ; i != len ; i++) arr[i] = condition
Little Alien
  • 1
  • 8
  • 22
2

Use a BitSet, for the final AND functionality:

BitSet bits = new BitSet(array.length);
for (int i = 0; i < array.length; ++i) {
    bits.set(i, ... true or false);
}

boolean allTrue = bits.cardinality() == array.length();
boolean allFalse = bits.cardinality() == 0; // Or bits.isEmpty()
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • is it better or should i just loop through array and use AND on them for simplicity. may not be efficient though. @JonK : eagle eyes – Shahensha Khan Aug 03 '16 at 08:18
  • BitSet has methods to find the next or previous set bit 1. However `cardinality()` yields the number of bits set to 1, so use that `allTrue`. For allFalse `isEmpty()` of `Set` also works. – Joop Eggen Aug 03 '16 at 08:22
  • I wouldn't worry about the performance of using a `BitSet` vs doing it manually - unless you're working on a list numbering in the millions it will be a micro-optimisation at best. Use whatever reads the most clearly to you. If `bits.cardinality() == array.length` is clear *to you* then use it, if not, don't. – JonK Aug 03 '16 at 08:22
  • yeah in my case it might be less then 10, so i better give it a manual go. – Shahensha Khan Aug 03 '16 at 08:24
1

I will agree that ArrayList is the simple and most straight-forward approach.

With that, you can still do that with simple array[], you just need to keep in mind that array[] size is fixed, if you want to increase it size you need to define a new array[] in a bigger size (usually you would double the size of the original) and copy all items from the original array to the new one. It is also good practice to keep an index to the last point in the array you were using.

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
  • No the array size is fixed and does not need to be changed in order to keep track of the Binary AND function. all i need is an array with fixed length but how do i add values to it sequentially like add() is my need of time. – Shahensha Khan Aug 03 '16 at 08:04
  • Then I didn't understand correctly the question, anyway, I see you got an answer. – Roee Gavirel Aug 03 '16 at 09:07