0

I have the array of images with a length of 6 (we call array first).I want to save this 6 values in another array with a length of 12 so that each value of the first array repeated twice in the second array and the values placed in random places of the second array .

For example : first array = {3,4,20,33,1,25}

The second array can be = {4,20,33,1,20,25,25,3,4,1,3,33}

public class PlayActivity extends AppCompatActivity {

public static boolean isTheSameOk = true;
final Random rnd = new Random();
int saverandom;
int dontsame = 0 ;
int [] allimages = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5,
        R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10, R.drawable.img11,
        R.drawable.img12, R.drawable.img13, R.drawable.img14, R.drawable.img15, R.drawable.img16, R.drawable.img17,
        R.drawable.img18, R.drawable.img19, R.drawable.img20, R.drawable.img21, R.drawable.img22, R.drawable.img23,
        R.drawable.img24, R.drawable.img25, R.drawable.img26, R.drawable.img27, R.drawable.img28, R.drawable.img29,
        R.drawable.img30, R.drawable.img31, R.drawable.img32, R.drawable.img33, R.drawable.img34, R.drawable.img35,
        R.drawable.img36, R.drawable.img37, R.drawable.img38, R.drawable.img39, R.drawable.img40};
int [] chooseimages = new int [6];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);


    //choose 6 image from 40 image we have for putting into buttons
    for(int counter = 0 ; counter<6 ; counter++){
        saverandom = rnd.nextInt(39);

        if(DontSameChoose(allimages[saverandom])){
            chooseimages[counter] = saverandom;
        }else {
            counter--;
        }
    }

    //put the select images in buttons


}
protected boolean DontSameChoose (int imageid){

    for(int c = 0 ; c<6 ; c++){
        if(imageid == chooseimages[c]){
            isTheSameOk = false;
        }
    }
    return isTheSameOk;
}
}

the first array is choose images that have the 6 id of the images and i want to save the values in the way i said above in the another array with the lenght of 12 .

5
  • And what you have tried so far ? Commented May 11, 2016 at 21:37
  • So copy the values twice, to different offsets in the target array, then shuffle the target array. Commented May 11, 2016 at 21:40
  • What are you stuck on? Commented May 11, 2016 at 21:41
  • @sᴜʀᴇsʜᴀᴛᴛᴀ Now i have the first array called it in my code chooseimages and it have the values but i dont know exactly how to put the values like i said above ... Commented May 11, 2016 at 21:42
  • @DanielHernández Done . Commented May 11, 2016 at 21:47

2 Answers 2

4

If you transform your arrays into ArrayList, here is a short solution:

ArrayList<Integer> secondList = new ArrayList<>(firstList);
secondList.addAll(firstList);
Collections.shuffle(secondList);
Sign up to request clarification or add additional context in comments.

1 Comment

Change a2 to secondList.
0

Use this code

   HashMap <Integer,Integer> mapa;
            mapa = new HashMap<>();
            int cont=0;
            int arr[] = new int[12];
            int arr2[] = new int[]{3,4,20,33,1,25};
            for(int i=0;i<12;i++){
                arr[i] = (int)Math.random()*100;
            }
            while(true){
                int random = (int)Math.random()*12;

                if(mapa.containsKey(random)){
                }else{
                    mapa.put(random, 1);
                    arr[random] = arr2[cont++];
                }
                if(cont >= 6){
                    break;
                }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.