Collections.fill ArrayList with multiple values, not the same value - android

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);
}

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;

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

Getting numbers from 1 to 4 in a random order

I want to get 4 random value(different from each other) 1 to 4 in Android.
I wrote a code like this. But the values aren't different from each other.
Random random = new Random();
int number, idSearch[]=new int[4];
number = random.nextInt(4);
idSearch[0] = number;
for (int i = 1; i < 4; i++)
{
number = (int) random.nextInt(4);
for (int j = 0; j <= i; j++)
{
if (idSearch[j] == number) {
number = random.nextInt(4);
}
}
idSearch[i] = number;
}
Where is error? Can you help me?
I reckon the easiest way of generating a List<Integer> with values from 1 to 4 (inclusive) in a random order would be to first create a List<Integer> with the initial values and shuffle it:
List<Integer> list = Arrays.asList(1, 2, 3, 4);
Collections.shuffle(list);
System.out.println(list);
>> [4, 1, 3, 2]
Keep in mind that this list cannot be added to or removed from.
If you would like it to be mutable, simply pass it to the constructor of an ArrayList:
List<Integer> mutableList = new ArrayList<>(list);
Remember to import java.util.*
If you are using JDK1.8 then you can generate numbers from 1 to 4 in random order, you can use new Random().ints() as shown below:
int[] randoms = new Random().ints(1,5).distinct().limit(4).distinct().toArray();
If you are using JDK1.7 or earlier, you need to use Collections.shuffle() rather than you re-inventing the shuffling the logic.
You have to create new object Random and pls use second for behind fist for.

How to Display the text in Reverse from char to String

I tried to display the text but it is displaying in wrong view. I want to display the Textview in reverse. I tried this code
String subMenuId = (String) key[position];
String subMenuName = subMenuTable.get(subMenuId);
for (int x = 0; x < subMenuName.length(); x++) {
int splitword = subMenuName.charAt(x);
char c = (char) splitword;
TextView product = new TextView(con);
product.setText(String.valueOf(c));
product.setRotation(-90);
holder.tv.setGravity(Gravity.CENTER_VERTICAL);
product.setTextSize(12);
holder.tv.setPadding(5,0,5,0);
product.setTextColor(Color.BLACK);
product.setTypeface(null, Typeface.BOLD);
holder.tv.addView(product);
}
Try the following
String reversedString = new StringBuilder("LIKE").reverse().toString()
Just reverse your loop
i.e.
for (int x = 0; x < subMenuName.length(); x++)
to
for (int x = subMenuName.length() -1; x >=0 ; x--)
Hope it helps
When you iterate from 0 ⇢ n first char get referred first and will be added to top. since you want it in reverse, iterate n ⇢ 0

how to get pixel color using byte array in Android

In my Android project,
Here is my code.
for (int x = 0; x < targetBitArray.length; x += weight) {
for (int y = 0; y < targetBitArray[x].length; y += weight) {
targetBitArray[x][y] = bmp.getPixel(x, y) == mSearchColor;
}
}
but this code wastes a lot of time.
So I need to find way faster than bitmap.getPixel().
I'm trying to get pixel color using byte array converted from bitmap, but I can't.
How to replace Bitmap.getPixel()?
Each Bitmap.getPixel method invocation requires a lot of resources, so you need to avoid the amount of requests in order to improve the performace of your code.
My suggestion is:
Read the image data row-by-row with Bitmap.getPixels method into a local array
Iterate along your local array
e.g.
int [] rowData= new int [bitmapWidth];
for (int row = 0; row < bitmapHeight; row ++) {
// Load row of pixels
bitmap.getPixels(rowData, 0, bitmapWidth, 0, row, bitmapWidth, 1);
for (int column = 0; column < bitmapWidth; column ++) {
targetBitArray[column][row] = rowData(column) == mSearchColor;
}
}
This will be a great improvement for the performace of your code

Using setMultiples to set indices in a given array to steadily increase

I need to configure a method using setMultiples(int[] A, int offset), that will allow me to set the remaining indices of my array (A) starting at the offset to increasing in multiples of 17. Example (17, 34, 51, etc.)
The follwoing code allows me to fill my array (A) with increasing multiples of 17:
for(int i=3; i<500; i++) { values [i] = 17*2; }
I cant figure out how to use setMultiples after looking it up.
Do like this to set the values in the multiples of 17.
int[] values = new int[500];
int offset = 3;
setMultiples(values, offset);
in setMultiples method
private void setMultiples(int[] values, int offset) {
for(int i=offset; i<500; i++) {
values[i] = 17*i;
}
}

How to change direction of drawing children of grid to Right_To_Left in Android

Im trying to use grid but I need to change direction of inserting children from ( left to right ) to ( right to left ). is there any way to do so , simple example would help me more.
Thanks in advance.
I wrote this. I think solve your problem
/** Returns inverted list by step that take. for example if our list is {1, 2, 3, 4, 5, 6,
* 7 ,8 ,9} and step is 3 inverted list is this: {3, 2, 1, 6, 5, 4, 9, 8, 7}
*/
public static <E> ArrayList<E> invert(List<E> source, int step){
List<E> inverted = new ArrayList<E>();
for(int i = 0; i < source.size(); i++){
if((i + 1) % step == 0){
for(int j = i, count = 0; count < step; j--, count++){
inverted.add(source.get(j));
}
}
}
//
// When (source.size() % step) is not 0 acts.this is for last of list. add last part
// of the source that wasn't add.
//
int remainder = source.size() % step;
if((remainder) != 0 ){
for (int j = source.size() - 1, count = 0; count < (remainder); j--, count++) {
inverted.add(source.get(j));
}
}
return (ArrayList<E>) inverted;
}
i guess the only way is creating a custom gridview, overriding the onLayout() method.
take a look here.
or maybe you can invert items for each row in the list adapter? like for a 3-columns grid, instead of
[1 2 3][4 5 6][7 8] -->
[3 2 1][6 5 4][null 8 7].
(i admit i never used gridview)
i have face same problem but finally solved using reset Array
Here change only u r column no = 3
ArrayList<String> tb_ith_sections_list = new ArrayList<String>;
tb_ith_sections_list = dbhelper.getArrayList();
int sectionCount = tb_ith_sections_list.size();
if(sectionCount > 0){
int rowCount =sectionCount/4;
int colCount ;
if(sectionCount > 4){
colCount=4;
}else{
colCount = sectionCount;
}
if(colCount>sectionCount){
colCount=sectionCount;
}
int k=colCount;
int m=0;
for(int j=0;j<rowCount;j++){
m=(j*colCount);
k=m+colCount;
if(k>sectionCount){
k=(sectionCount-(j*colCount));
}
for(int i=m;i<k;i++){
TB_IVN_SECTIONS tb_Temp=new TB_IVN_SECTIONS();
TB_IVN_SECTIONS tb_ithFirst=tb_ith_sections_list.get(i);
TB_IVN_SECTIONS tb_ithSecond= tb_ith_sections_list.get(k-1);
tb_Temp=tb_ithFirst;
tb_ith_sections_list.set(i, tb_ithSecond);
tb_ith_sections_list.set(k-1,tb_ithFirst);
k--;
}
}

Categories

Resources