-1

I have a class attribute which is an Anytype built-in array. At some point I need to reallocate it’s memory to double it, but since it is Anytype, I can’t do:

myArray = new Anytype[myArray.length * 2];

Since the array is already defined, I can access to it’s elements class:

myArray[0].getClass();

My question is: Can I use the fact that I know the class of my array to reallocate it? I know I could use Arrays.copyOf from Java utils, but I want to know if it is possible to reallocate knowing the size your object need, kind of a C-way:

myArray = new myArray[0].getAllocationNeeded()[myArray.length * 2];

P.S: I’m not sure if the title is relevant, please indicate me if you have something more clear

Ulysse BN
  • 6,678
  • 2
  • 37
  • 63
  • You want a dynamic length array? look at ArrayList – Gordon Feb 09 '17 at 06:39
  • "I can’t do: `myArray = new Anytype[myArray.length * 2]`" => Why? This is exactly how it is done. What does the elements type have to do with the size of the array? – Seelenvirtuose Feb 09 '17 at 06:39
  • @Seelenvirtuose, probably, he doesn't want to lose data which keeps there – Andrew Tobilko Feb 09 '17 at 06:40
  • @Gordon can’t do sir, I know it’s the right way however i **need to implement it with built-in array**. – Ulysse BN Feb 09 '17 at 06:40
  • 1
    @AndrewTobilko Then the reference to the _new_ array must be assigned to another variable, then you can copy the content. This question is unclear! – Seelenvirtuose Feb 09 '17 at 06:41
  • @UlysseBN you can create a property like thing to update the "built-in array" – Gordon Feb 09 '17 at 06:43
  • @Seelenvirtuose Well the compiler throwed us an error for it, unfortunately I don’t have the code with me... I’ll upload it as soon as possible. So you mean `Anytype` guess the needed size? Anyway this is not about keeping my array’s data (I’m already making a copy of it) – Ulysse BN Feb 09 '17 at 06:43
  • 1
    @UlysseBN You seem to think that the type (here: `AnyType`) has any influence on the size of an array. **It does not!** And if you already tried something out and failed, you have to put it into your question. – Seelenvirtuose Feb 09 '17 at 06:44
  • @Seelenvirtuose Ok my bad, my error must come from somewhere else... I’ll come back with an MCVE – Ulysse BN Feb 09 '17 at 06:47
  • @Gordon I don’t get what you’re saying.. – Ulysse BN Feb 09 '17 at 06:48
  • 1
    You may be still thinking in C. In Java the size of the array type has no bearing on the size of the array, because the array contains only _references_ to the array elements. You never multiply the number elements by the size of an element, the way you would do in C. – Jim Garrison Feb 09 '17 at 07:06
  • I can update my question, i _finally_ have the code back! Though I think I’ll create a new one, since it is different than this one (not for copy but initialization of an `AnyType` array in a class with this kind of signature: `class ArrayQueue`) – Ulysse BN Feb 21 '17 at 23:47

1 Answers1

0

Yes, something like this (assume source is not null or empty):

   public <T> T[] resizeArray(T[] source, int multiplier) {
    T[] newArray = (T[]) Array.newInstance(source.getClass().getComponentType(), source.length * multiplier);
    System.arraycopy(source, 0, newArray, 0, source.length);
    return newArray;
}

Both:

  • source.getClass().getComponentType()
  • myArray[0].getClass()

Return the element class.

CroMagnon
  • 1,210
  • 7
  • 20
  • 32
Helen Lam
  • 26
  • 2
  • Thanks, according to comments that may not be what I’m looking for, but it’s definitely what I thought I would get. I hope your downvoter will explain himself, so we know the issue if there is. – Ulysse BN Feb 09 '17 at 08:14
  • Ok now that I solved my problem, I can accept your answer : I used it. And I also used [this answer](http://stackoverflow.com/a/2924453/6320039) for the array initialization. – Ulysse BN Feb 23 '17 at 21:56