What's difference between
String [] O = {};
and
String[] Ox = new String[3000];
How can I copy the strings from Ox to O?
O is empty array and Ox has 3000 length, to copy Ox to O you have to use copyOf() api of Arrays class.
O = Arrays.copyOf(Ox, Ox.length);
Arrays.copyOf() create change the O array length to 3000 and copy all contents.
public static void main(String[] args) {
String [] O = {};
String[] Ox = new String[3000];
O = Arrays.copyOf(Ox, Ox.length);
System.out.println("O array length : "+O.length); // I am just printing length
}
Output :
O array length : 3000
Internal implementation of copyOf() api of Arrays class.
public static char[] copyOf(char[] original, int newLength) {
char[] copy = new char[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
Array is immutable so you can not assign 3000 size array to size 0 array, in implementation of copyOf() method java created new array char[] copy and using native method copy content of original array to copy array.
So, in you code you can not directly copy Ox array contents to O array for that you have to create array of size 3000 or use copyOf() api of Java.
With the following statement you declare a table O that is empty (size=0, no elements).
String[] o = {};
If instead you write:
String[] o = {"this", "is", "a", "test"};
you have initialized table O to have size=4 and contain the four string elements "this", "is", "a" and "test".
Finally, if you initialize your table in the following way:
String[] o = new String[4];
you have declared that your array has size=4, but all four elements are null. Of course, you can later change the values of your array (eg. o[3] = "test").
Now, if you want to copy the elements of one array to the other you can use the Arrays.copyOf(o, o.length); method, but in the following way :
String[] o = {"this", "is", "a", "test"};
String[] copy; // you don't have to (and shouldn't) initialize this variable to {}
copy = Arrays.copyOf(o, o.length);
There are a couple of differences here:
1) String [] O = {};
this means to create a new String[] called O which is initialized with the data set contained in {}. Since there is no data, O is "empty".
This means that O.length = 0, or put another way, this Array has no memory associated with it for storage purposes.
Since Arrays are immutable in java, you cannot do anything further with this array.
You could have done:
String[] O = {"bob","Cat","ralph"}
O.length // == 3
O[0] // bob
O[1] // Cat
O[2] // ralph
O[1] = "Sting"
O[1] // now set to Sting
O[4] = "Cause an Exception" // causes an index out of bounds exception
2) String[] O = new String[3000];
In this case you have created a new String[] with 3000 "spaces" or "indices" available. What this really means is that you can maintain references to 3000 String objects in this array.
So:
O.length; //3000 because that is the length of this array
O[0] // will return null since we have not stored anything here
O[0] = "My String"
O[0] // now equals "My String"
O[3001] // throws an index out of bounds exception
3) How can you copy between them:
In short you cannot in this case. Since array 1 (String[] O = {}) is empty and has a length of 0 any attempt to copy to or from this array will result in an exception.