-2

I have a two dimensional array, e.g. String array[][] = {{"","","","","",},{"","","","","",}}. This array is full and I want to resize it. So since arrays are fixed in size, I want to copy this array into a new, bigger array.

Also, I searched around the site and found that I can use System.arraycopy() and other methods but I prefer only using for loops. I was thinking a couple of for loops will suffice.

What I tried was:

String[][] newArray;
  for(int i=0; i<array.length; i++) {
    for(int j=0; j<array[i].length; j++)
      newArray[i][j]=array[i][j];
      System.out.println(newArray[i][j]);
      System.out.println();
    }

But I keep getting a compiling error saying error: cannot find symbol j?

Also, I get the message variable newArray might not have been initialized. Does newArray must be initialized because I don't know what the size of it will be after my code is finished?

Robben
  • 251
  • 3
  • 11
  • You have a full array? Then use a `Collection` instead. – Tom Feb 06 '15 at 00:19
  • What is your question? – Andy Turner Feb 06 '15 at 00:20
  • At a first glance, you might've dropped a bracket at the end of the nested `for`. After fixing that, it will throw a `NullPointerException` since you're trying to assign to `newArray` which isn't initialized. – Attacktive Feb 06 '15 at 01:45
  • @Suzi does it have to be initialized? Because I don't know what the size of this larger array will be? And I don't see any dropped bracket? – Robben Feb 06 '15 at 01:53
  • If you can't decide the size, consider using 2-D [ArrayList](http://stackoverflow.com/a/10768198/1986241). Also, if you haven't missed an opening bracket there, are you sure you meant [this](http://paste.ofcode.org/34k74DHvhZzqE8SjyjXjhSk)? – Attacktive Feb 06 '15 at 02:35

1 Answers1

1

As to why your question gets down-voted: you state in your question that you'll probably be using two loops to copy everything into a bigger array. This way it seems like you're answering your own question, which would render your post unnecessary in the first place.

Also, built in functions are provided for a reason. Generally they are way more efficient and performant than home made solutions.

Now, on to your issue. Yes, you could use two loops is you absolutely want to avoid the built in arraycopy function. As you pointed out this solution yourself, I probably won't have to explain how that works.

Just be aware that there are better ways to handle such datasets. The collections mentioned by Tom are way easier to work with as they can scale dynamically, or you could even opt for 1D collections of objects if your data does allow for this.

  • But having the idea of using two loop doesn't mean you already know how to apply them. I having difficulty applying them. I agree, built in functions are awesome but as a noob like me I need to understand the basics first. – Robben Feb 06 '15 at 01:08
  • Oh, no problem. Now, I'm currently on mobile and my Java is a bit rusty, so I'm not going to be able to write down a full code example. I can however explain the basic idea. Let me edit that in for you. –  Feb 06 '15 at 01:12
  • 1
    I just lost everything while typing on my phone. I'll check in on this question tomorrow, and if no one has provided an answer by then I'll give you the full explanation on how to do it.. I'm sorry I can't be of more help right now. –  Feb 06 '15 at 01:34