7 Replies - 3045 Views - Last Post: 23 October 2010 - 09:27 PM Rate Topic: -----

#1 Guest_Mario*


Reputation:

Random objects from arrays = newbie help.

Posted 23 October 2010 - 08:22 AM

Hey all!

Yes, I'll admit, I am a newbie in Java. However, now my brains got fried on this thing - picking random objects from wherever (example of an Array). :dozingoff:

Before detailing, my main question is: how do you use Random generator on objects (arrays, arraylists etc.)? (Not Int, Not String) I.e. how do you converts objects so that the Random generator can pick them?

Assume following:
Class Bankaccount
Objects Bankaccounts (with attributes like balance, id etc.) - these are stored in an array.

How can I pick, say 3 random objects (bankaccounts) from an array of say 10 bankaccounts?

Code example:

import java.util.*;
import java.util.Random;

class Bankaccount{
	private int balance;
	
	public void setBalance(int a){
		balance = a;
	}
	public int getBalance(){
		return balance;
	}
}

//Alright, I am thinking of creating a new class which has a method that calls on the array of type Bankaccount and stores it
//in whatever area, for example another array or arraylist.

class pickRand{
	public static Bankaccount [] get(Bankaccount[]array){
		Random rand = new Random();
		int rnd = rand.nextInt(array.length);
		
		
		//Objects to int - how to? Any hints?
		//Can a solution be to attack the balance variable of objects?
		
		//The other part of adding those objects is ok, and also how many to pick.
	}
}


public class Account{
	public static void main(String []args){
		Bankaccount b1 = new Bankaccount();
		Bankaccount b2 = new Bankaccount();
		Bankaccount b3 = new Bankaccount();
		Bankaccount b4 = new Bankaccount();
		Bankaccount b5 = new Bankaccount();
		Bankaccount b6 = new Bankaccount();
		Bankaccount b7 = new Bankaccount();
		Bankaccount b8 = new Bankaccount();
		Bankaccount b9 = new Bankaccount();
		Bankaccount b10 = new Bankaccount();
		b1.setBalance(10000);
		b2.setBalance(30000);
		b3.setBalance(13000);
		b4.setBalance(36000);
		b5.setBalance(19000);
		b6.setBalance(28000);
		b7.setBalance(30000);
		b8.setBalance(30000);
		b9.setBalance(15000);
		b10.setBalance(30000); //Yes, very thorough, but look up! :)/>
		
		Bankaccount [] bac = new Bankaccount [10];
		bac[0] = b1;
		bac[1] = b2;
		bac[2] = b3;
		bac[3] = b4;
		bac[4] = b5;
		bac[5] = b6;
		bac[6] = b7;
		bac[7] = b8;
		bac[8] = b9;
		bac[9] = b10;
	
	//The final output looks something like this...
		Bankaccount[]bac2 = pickRand.get(bac);
		System.out.println(bac2);//and so on.
	}
}


Any hints will be greatly appreciated! However, please keep me in the Random generator area!

Is This A Good Question/Topic? 0

Replies To: Random objects from arrays = newbie help.

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2700
  • View blog
  • Posts: 11,368
  • Joined: 20-September 08

Re: Random objects from arrays = newbie help.

Posted 23 October 2010 - 08:30 AM

Personally i would use List<BankAccount> then you don't have the problem of choosing the same object more than once

List<BankAccount> allAccs = new ArrayList<BankAccount>((List<BankAccount>)Arrays.asList(bac));
BankAccount randAcc = allAccs.remove((int)(Math.random() * allAccs.size()));


This post has been edited by g00se: 23 October 2010 - 08:31 AM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Mario*


Reputation:

Re: Random objects from arrays = newbie help.

Posted 23 October 2010 - 11:10 AM

g00se

Thx for the answer!
However I need to stay away from Math.method, and the unique bankaccount issue can be solved through a series of while or if loops (more coding but works).
I am looking for the answer of how Random generator can handle objects, so that I can use it in the future as well.

Thanks again!

Welcoming more suggestions!
Was This Post Helpful? 0

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2700
  • View blog
  • Posts: 11,368
  • Joined: 20-September 08

Re: Random objects from arrays = newbie help.

Posted 23 October 2010 - 11:15 AM

Quote

I am looking for the answer of how Random generator can handle objects


I'm not sure what you mean. Are you referring to java.util.Random?
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Mario*


Reputation:

Re: Random objects from arrays = newbie help.

Posted 23 October 2010 - 11:41 AM

g00se

Yep, that's the sugar!
How it can use objects is the thing for me!
Was This Post Helpful? 0

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2700
  • View blog
  • Posts: 11,368
  • Joined: 20-September 08

Re: Random objects from arrays = newbie help.

Posted 23 October 2010 - 12:09 PM

More or less the same:


Random r = new Random();
BankAccount randAcc = allAccs.remove(r.nextInt(allAccs.size()));

Was This Post Helpful? 0
  • +
  • -

#7 Guest_Mario*


Reputation:

Re: Random objects from arrays = newbie help.

Posted 23 October 2010 - 12:58 PM

g00se

Yes! It works! Now I'll rewrite it 20 times so it'll stick!
Thanks a lot!
Was This Post Helpful? 0

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random objects from arrays = newbie help.

Posted 23 October 2010 - 09:27 PM

public class Account{
	public static void main(String []args){
		Bankaccount b1 = new Bankaccount();
		Bankaccount b2 = new Bankaccount();
		Bankaccount b3 = new Bankaccount();
		Bankaccount b4 = new Bankaccount();
		Bankaccount b5 = new Bankaccount();
		Bankaccount b6 = new Bankaccount();
		Bankaccount b7 = new Bankaccount();
		Bankaccount b8 = new Bankaccount();
		Bankaccount b9 = new Bankaccount();
		Bankaccount b10 = new Bankaccount();
		b1.setBalance(10000);
		b2.setBalance(30000);
		b3.setBalance(13000);
		b4.setBalance(36000);
		b5.setBalance(19000);
		b6.setBalance(28000);
		b7.setBalance(30000);
		b8.setBalance(30000);
		b9.setBalance(15000);
		b10.setBalance(30000); //Yes, very thorough, but look up! :)/>
		
		Bankaccount [] bac = new Bankaccount [10];
		bac[0] = b1;
		bac[1] = b2;
		bac[2] = b3;
		bac[3] = b4;
		bac[4] = b5;
		bac[5] = b6;
		bac[6] = b7;
		bac[7] = b8;
		bac[8] = b9;
		bac[9] = b10;
}


You like to write code for nothing or what ? :)
public class Account {
   public static void main(String[] args) {
      int[] balance = {1000, 1500, 1300, 100, 22345, 12334, 123, 12000, 987654};
      Balanceaccount bac[] = new Balanceaccount[balance.length];
      for(int i = 0; i < balance.length; i++) {
          bac[i] = new Balanceaccount();
          bac[i].setBalance(balance[i]);
      }


:^:

This post has been edited by pbl: 23 October 2010 - 09:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1