Home > Enterprise >  Collections.fill ArrayList with multiple values, not the same value
Collections.fill ArrayList with multiple values, not the same value

Time:01-04

I'm using Android Studio where I have an array with a size of 126, and I initially fill them with 0's. Then I have an input with a size of 63, I want it to "replace" the first 126 values, instead of adding 63 to the 126.

For example I have an array of length 5 ( [0,0,0,0,0] ). Then I input 1,2,3 as individuals. I want it to look like [1,2,3,0,0] instead of [0,0,0,0,0,1,2,3]

example code:

ArrayList<Float> list = new ArrayList<Float>(Collections.<Float>nCopies(126, Float.valueOf(0)));

Then I add by (edited):

for (int j = 0; j < loop; j  ) {
         float xx = result.multiHandLandmarks().get(i).getLandmark(j).getX();
         floaat yy = result.multiHandLandmarks().get(i).getLandmark(j).getY();
         float zz = result.multiHandLandmarks().get(i).getLandmark(j).getZ();
        list.add(xx);
        list.add(yy);
        list.add(zz);
}

CodePudding user response:

When you use

list.add(x.get(i))

it will be added to the end of the array. use this :

list.add(i , x.get(i))

the first parameter is index, second is the value;

CodePudding user response:

You don't need to add, that's the problem, you need to set. For example:

for (int i = 0; i < newList.size(); i  ) { //like i < 63
                        oldList.set(i, newList.get(i)); //your new value
                    }
  • Related