I know you can't dynamically expand a normal array but is this a valid way of doing it?
public int size = 0;
public String[] OrigArray = new String[size+1];
public void expand(){
String[] tempArray = new String[size+1];
tempArray = (String[])OrigArray.clone();
OrigArray = new String[size+1];
OrigArray = (String[])tempArray.clone();
size++;
}
I'm aware of much better methods than trying to use a normal array but I'd like to figure this for just using a normal array first.
My desire is that it starts off with OrigArray
being 0+1 (so 1) and when expand()
is called the new tempArray
is made that is the same size as the OrigArray
and this then holds OrigArray
while OrigArray
is declared again with size+1
then the tempArray
is copied back to the newly sized OrigArray
. This makes sense to me, but I keep getting out of bound exception?
We know that HashSet doesn't allow duplicate values in it. We can make use of this property to check for duplicates in an array. The idea is to insert all array elements into a HashSet . Now the array contains a duplicate if the array's length is not equal to the set's size.
You got two options to do this. First, as what @bholagabbar said, use ArrayList instead. Option 2, you can double the array size, copy the old array into the new one (which is similar to what ArrayList does under the hood). BTW, adding one extra space is not a good idea to expand an array.
The method does not change the value of OrigArray; all it does is store a clone of a clone in it, so in effect the value isn't changed.
I think what you want is this:
public void expand() {
String[] newArray = new String[OrigArray.length + 1];
System.arraycopy(OrigArray, 0, newArray, 0, OrigArray.length);
//an alternative to using System.arraycopy would be a for-loop:
// for(int i = 0; i < OrigArray.length; i++)
// newArray[i] = OrigArray[i];
OrigArray = newArray;
}
This creates an array that has a size 1 greater than OrigArray, copies the content of OrigArray into it and assigns that array to OrigArray. Unless you want to remember how many times expand()
has been called, there shouldn't be a reason to have the variable size
.
EDIT: If what you really want is to know a way to sensibly implement the functionality you asked for, you can go with what @Óscar López said and use ArrayList.
What you're trying to accomplish by hand, it's pretty much what ArrayList
does for you - use that class, instead.
Under the hood, ArrayList
uses an Object[]
for storing items, under a certain capacity constraint. When the array is filled (as new items are added), a new array with doubled size is created and all the items in the original array are copied in it. All this happens automatically, and it's transparent for the programmer.
Given that in the sample code you're storing an array of objects (Strings), there'll be little difference in performance if you use an ArrayList
for storing them, so there's no real reason to reinvent the wheel!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With