• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

global arrays

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

i have an int array for example :  arr = {10,13,23,14}.

I want to shuffle the numbers of arr and put it in arr2. (so for example the arr2 = {23,13,10,14}

then i want to use the arr2[0] , arr2[1] in any other class i want.

Basically i dont want to use the same number twice.

Is it possible?

 
Saloon Keeper
Posts: 12811
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Sure, it's possible. What have you tried? What are you stuck on?
 
Master Rancher
Posts: 4251
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are the other classes in the same activity with the array?
 
Yahel Perman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again and thanks for your replies.

Im creating a multiple choice quiz game and i want to shuffle the answers.
Currently my code is like this



where [a] is the array with questions and [0] is the position of the answers array

so i want the shuffling array to be outside the button class.
Ideally i want something like.



where the shuffle.array[0] will be a shuffled array of integers
 
Marshal
Posts: 72423
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please indent your code and use the code button; I went back to your code and added code tags and doesn't it look better now

Beware of passing arrays around. Arrays in Java┬« are always mutable and all it takes is one line of code saying myArray[i] = xyz; and you have broken your code everywhere you are using that array. You can copy arrays with their clone() method.
I don't know of a shuffling technique readily available in Java® (Android might be different) but you can shuffle Lists (link) or you can copy that technique to write your own shuffle() method.
 
Yahel Perman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again!

Im using the collection.shuffle or the Fisher-Yates shuffle directly. The problem is that, if i call the shuffle() method inside the buttons class, reshuffles the answers everytime with result to have same answers in different buttons. Thats why i need to shuffle the array once (outside the button classes or maybe in different .java file) and then call the shuffled array indices one by one.

like

shuffle.java


buttons.java
 
Campbell Ritchie
Marshal
Posts: 72423
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your shuffle technique doesn't shuffle the array everywhere. If you only use one array and don't take copies of it, you only have one array and it has the same state everywhere. Not, “shuffle everywhere,” but, “shuffle once, see the result everywhere.” You need to take copies of the array wherever you use it.
 
Yahel Perman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right now im not in frond of my working PC so i cant test it.

But as i understand, you suggest shuffling the array inside the first button class, and then using the copy.array[1] inside the second button class?
 
Campbell Ritchie
Marshal
Posts: 72423
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that what you want? I would have thought take a copy of thee original array and shuffle the copy. Each object getting an array should copy it immediately. That way everywhere the array is used has its own copy and they will behave as independent of one another. The problem with global variables is that changes made to them are reflected everywhere.
 
Don't MAKE me come back there with this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic