0

Say I have something like this:

public void handle(ArrayList<Object> tasks) {

    ArrayList<Object> results = new ArrayList<Object>();

    for (int i = 0; i < tasks.size(); i++) {
        results.set(i, tasks.get(i);
    }
}

if I do the above I will get an error:

Index 0 out-of-bounds for length 0

so the temporary/dumb solution I have is to do:

  for (int i = 0; i < tasks.size(); i++) {
      results.add(null);
      results.set(i, tasks.get(i);
  }

but that is awful lol...how do we do this right with Java?

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 6
    1 liner `ArrayList results = new ArrayList<>(tasks);` – Ori Marko Jan 28 '19 at 10:13
  • that's cool, but that's duplicating a lot of object references that I am going to end up discarding, I'd rather just put a bunch of nulls in there? –  Jan 28 '19 at 10:16
  • also, what if tasks has 100K items, seems expensive –  Jan 28 '19 at 10:16
  • IMO, having a list with 100K, is itself expensive. – Nicholas K Jan 28 '19 at 10:17
  • Thaz true, pretty rare case, then again we have linked-lists doing crazy optimization since traversing big arrays recursively/compound is hard, if this was part of some recursive thing it could get nasty, it's all about that big-O –  Jan 28 '19 at 10:19
  • @rakim Maybe List isn't optimal data structure for you, but there's little information to tell. – Ori Marko Jan 28 '19 at 10:19
  • Yeah it just seems unnecessary to have to copy all the data from one array to a new one to initialize properly, I think you'd agree. With JS you wouldn't have to do anything like this. You'd just be like `const z = []; z[3] = "salamander"`. –  Jan 28 '19 at 10:21
  • So this works, not sure if it's that much better tho `List results = new ArrayList(Collections.nCopies(tasks.size(), 0));` –  Jan 28 '19 at 10:23
  • just `add` instead of `set` (`set` is for replacing an item under certain index, and `add` will put item at the end of the list which in this case is at index `i`). You may consider using `Map` and `put`. Another solution is to use java array `Object[] array = new Object[tasks.size()]` - if you know exact size of the array before, this one will be the most efficient. – RadekJ Jan 28 '19 at 10:25

2 Answers2

0
 for (Object task:tasks) {
        if(task==null) {
        results.add(task)
       }      
    }
BaoTrung Tran
  • 392
  • 2
  • 6
  • 21
  • what about `List results = new ArrayList(Collections.nCopies(tasks.size(), 0));` –  Jan 28 '19 at 10:24
  • Why you need add 0 in to results ??? Method you post mean : You want add tasks.size() with 0 value. It mean : Example : tasks you contain 5 element, after method run result is : 0,0,0,0,0 . task.size() is capacity and 0 is value you want add. – BaoTrung Tran Jan 28 '19 at 10:35
  • You can read it here : https://stackoverflow.com/questions/5600668/how-can-i-initialize-an-arraylist-with-all-zeroes-in-java – BaoTrung Tran Jan 28 '19 at 10:36
0

set() is used for replacing a previously added element, hence why you got an error in your first approach. And in your second approach why not just use results.add(tasks.get(i)) instead of first adding a null object and replacing it with set()