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.
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 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;
}
}
int[] images2 = {
R.drawable.wrong, R.drawable.reviewp,
R.drawable.bowl, R.drawable.ic_action_search
};
List<int[]> pic=Arrays.asList(images2);
Collections.shuffle(pic);
Random random = new Random();
for(int i = 0 ; i <= 3 ; i++){
ReciverI[i].setBackgroundResource(images2[ "at here i want to get the image position" ]);
}
help me to get the position of image
this is not working
ReciverI[i].setBackgroundResource(images2[Integer.parseInt(pic.get(i).toString())]);
this give me wrong result
I rewrite the code
Solution 1
Integer[] images2 = {R.drawable.wrong, R.drawable.reviewp, R.drawable.bowl,R.drawable.ic_action_search};
List<Integer> pic=Arrays.asList(images2);
Collections.shuffle(pic);
for(int i = 0 ; i <= pic.size() ; i++){
ReciverI[i].setBackgroundResource(pic.get(i))
}
You want absolutely to conserve the primitiv int then you have to make a little walk around
Solution 2
List<Integer> pic = new ArrayList<Integer>();
for (int index = 0; index < images2.length; index++)
{
pic.add(images2[index]);
}
Collections.shuffle(pic);
for(int i = 0 ; i <= pic.size() ; i++){
ReciverI[i].setBackgroundResource(pic.get(i))
}
ReciverI[i].setBackgroundResource(pic.get(i))
Sorry wasn't paying attention. You should just make your List<int> and it will work.
As your snippet is now, it has no use to have a List of integer arrays.
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--;
}
}