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;
}
}
Related
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
}
everyone, Please help me!
I want to show an array of numbers in a shape.
What I did
1) I draw a shape with canvas in View class.
2) I create a single random number.
3) I've searched a lot & find out that should use Drawtext to show a text or number.
4) I've read the documentation of Random, Drawtext, etc.
5) I've tried for loop but it didn't work outside of canvas class & inside, also will repeat that single number infinite times.
my Problem
I don't know how to put that number in an array & how to show array with drawtext in view class.
at the moment, I can show only one random number, but a want to show an array of random numbers.
I'm very new in Android & my English is not so good.
I'll be very grateful if anybody can Help me through this.
Thank You.
Here's the part of my code that I used for creating a single random number (this is outside of class, constructor & onDraw method) :
Random rand = new Random();
int number = rand.nextInt(100) + 1;
String mystring = String.valueOf(number);
& in onDraw method for showing this number i used below code :
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText(mystring,130,480,black_paintbrushstroke);
}
Please try to use ArrayList to hold your random data, like:
ArrayList<Integer> arrayList = new ArrayList<>(100);
Random r = new Random(System.currentTimeMillis());
for(int i = 0; i < 100; i++) {
arrayList.add(r.nextInt());
}
Then:
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
int yOffset = 10;
int yStartPosition = 480;
for (int i = 0; i < arrayList.size(); i++) {
Integer integer = arrayList.get(i);
canvas.drawText(String.valueOf(integer), 130, yStartPosition+(i*yOffset), black_paintbrushstroke);
}
}
Looks like you are drawing all your items into the same postion (x,y), you need to add some vertical offset to draw new value bellow previous one.
If you want to draw all you numbers as comma-separted string, you need to convert your array into String using StringBuilder:
StringBuilder sb = new StringBuilder();
for(int i = 0; i < arrayList.size(); i++) {
sb.append(arrayList.get(i)).append(",");
}
String myRandomNumbersArray = sb.toString();
sb.setLength(0);
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.
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
Now I have 2 dimension array that collects all color's pixel
and I have 1 dimension array that collects a specific color that every pixel need to check with this array. But how to check this 2 arrays
first array is
array_A = new String[bitmap.getWidth()][bitmap.getHeight()];
Another is final String[] array_B = { "ffcc33","ffcc00",....} so how can I check this 2 arrays :)) Thanks in Advance
Hope that snippets would help you. Also, you can use pixel[] = new int [width*height] to get pixel from image.
for(int w= 0; w < bitmap.getwidth(); w++)
{
for(int h = 0 ; h < bitmap,getheight() ; h++ )
{
int c = bitmap.getPixel(w, h);
Log.i("Pixels", h+"X"+w);
}
}