-2

I need to fill a whole load of fixed size arrays quickly. My thinking is this:

public void fillArrays(){
    int[] filler_array = {0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    System.arraycopy(filler_array, 0, spikeWaveB1, 0, 22);
    filler_array = {0xF4, 0x26, 0x58, 0x8A, 0xBC, 0xEE, 0x20, 0x52, 0x84, 0xB6, 0xE8, 0xF4, 0xC2, 0x90, 0x5E, 0x2C, 0xFA, 0xC8, 0x96, 0x64, 0x32, 0x00};
    System.arraycopy(filler_array, 0, spikeWaveB2, 0, 22);
    filler_array = {...  ...};
    System.arraycopy(... ...);
}

Essentially I need a way of filling an already populated array with {x,y,z}. How do I re-fill it with a new list of values?

Any help greatly appreciated. Regards, George

G3or6311
  • 66
  • 5
  • 3
    `filler_array = null` will not empty the array. – bradimus Jun 21 '17 at 14:02
  • Does https://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java and/or https://stackoverflow.com/questions/20316526/combine-two-integer-arrays-into-one-array-in-java provide any assistance? – ZeldaZach Jun 21 '17 at 14:02
  • Thanks, I'll look into it further... I'm afraid not, I'm not trying to combine two arrays. I need to put a new list of values ({x,y,z}) starting at [0] into an already existing array after getting rid of the existing values – G3or6311 Jun 21 '17 at 14:04
  • So where will this new list of values come from? – Gimby Jun 21 '17 at 14:04
  • @Gimby if you look at the 4th line in the example code you will see what i'm trying to achieve. – G3or6311 Jun 21 '17 at 14:11
  • Counting from the top line 4 assigned a new array to the existing 'filler_array' local variable. What is wrong about that line? Functionally it achieves what you want, spikeWaveB2 will receive a different set of values. – Gimby Jun 21 '17 at 14:21
  • @Gimby That is what I thought... except it doesn't work! That's what I couldn't understand, I get an "Array Initializer not allowed" error. However it is a moot point as Stephen below has stated that refilling arrays is inefficient and should be avoided. Many thanks for your time. – G3or6311 Jun 21 '17 at 14:37
  • Ah indeed, you must create a new array. For future reference. `filler_array = new int[] {values here}`; The reason you do not need to do that the first time is because it is in the variable initialisation. It may seem dumb that you need to manually create a new array the second time, but Java has may rigid rules that have to do with guaranteeing you're not making programming mistakes. – Gimby Jun 22 '17 at 07:39
  • Also for future reference regarding asking questions on this site: if you had added that error right away in your question, you probably would have had an answer in minutes and would maybe not have had downvotes. – Gimby Jun 22 '17 at 07:42
  • @Gimby thanks for the advice, on both fronts. That makes perfect sense. Thanks for taking the time to get back to me. – G3or6311 Jun 22 '17 at 07:46

2 Answers2

2

TL;DR - refilling is inefficient. Avoid doing it.

Move the filler arrays out of the class and create a separate variable for each one. For example:

private static final int[] filler_array = {
    0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03,
    0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
private static final int[] filler_array2 = {
    0xF4, 0x26, 0x58, 0x8A, 0xBC, 0xEE, 0x20, 0x52, 0x84, 0xB6, 0xE8,
    0xF4, 0xC2, 0x90, 0x5E, 0x2C, 0xFA, 0xC8, 0x96, 0x64, 0x32, 0x00};

public void fillArrays(){
    System.arraycopy(filler_array, 0, spikeWaveB1, 0, 22);
    System.arraycopy(filler_array2, 0, spikeWaveB2, 0, 22);
    // etcetera
}

Reasoning: if you initialize the filler_array inside the fillArrays, then the initialization code is executed each time you call the method. (The array initializer you are using is most likely going to perform a copy behind the scenes.)

By using variables declared at the class (or instance) level, the initialization happens once at class initialization time (or instance initialization time).

Also, given the way that you are using filler_array (as the source for a copy) you are achieving almost nothing by copying data over it.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Try using an ArrayList and then at the end convert it into an array.

List<Integer> list = new ArrayList<>();
.... // your inserts into the list here
int[] filler_array = list.toArray();

Much easier to add new elements that way.

Inxsible
  • 700
  • 5
  • 27