0

For example i want to make 10 Random object in java, but this code doesn't work, because an object name can't be an integer.

for(int i = 0; i < 10; i++)
{
    Random i = new Random();
}

Any ideas?

5
  • 6
    You're trying to use i as two different things. Use a different variable name. Commented May 4, 2018 at 13:22
  • I'm voting to close because "This question was caused by a problem that can no longer be reproduced or a simple typographical error" Commented May 4, 2018 at 13:24
  • Answered and voted to close Commented May 4, 2018 at 13:26
  • I may take a beginner book of programming. There is nothing specific to java in your problem. Commented May 4, 2018 at 13:27
  • Why don't you use a array of Random with length 10? Commented May 4, 2018 at 13:27

4 Answers 4

2

You could build them into a List:

    List<Random> randoms = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        randoms.add(new Random());
    }

or an Array:

    Random[] randoms = new Random[10];
    for (int i = 0; i < randoms.length; i++) {
        randoms[i] = new Random();
    }

There are other options but these are the most common.

Sign up to request clarification or add additional context in comments.

Comments

1

Use

for(int j = 0; j < 10; j++)
{
    Random i = new Random();
}

instead should work.

But if it's me I'd like to use Stream.generate(Random::new).limit(10).

4 Comments

I don't think this will result in 10 usable Random instances.
@mustaccio He just want to create objects... I just helped him to make his code compile.
I think to be honest, try using a HaspMap() to create your object in a for loop. RandomMap.put(i, new Random(i)); This will help you get back, your objects in this way > RandomMap.get(i);
If it's me I'd like to Stream.generate(Random::new).limit(10)
0

This is easy method

List<SomeClass> someClass = new ArrayList<>();
for (int i = 0; i < 100; i++) {
    someClass.add(new SomeClass());
}

Comments

0

When you have written this code.

for(int i = 0; i < 10; i++)
{
    Random i = new Random();
}

You were thinking that multiple object of Random class will be created by the name 0,1,2,3,4.......9 but it does not work that way.

If you would have use something like this:-

for(int i = 0; i < 10; i++)
{
    Random "obj"+i = new Random();
}

Then also it will not work.

If you have ever work with String class then you know that in order to create multiple object of String you need to do something like this.

    String s1,s2,s3;
   // and then 
    s1="first String ";
    s2="second String";
    s3="third String";//and so on

Similar think can be done with Random:-

Random r1,r2,r3;
r1=new Random();
r2=new Random();
r3=new Random();

But if you want to create many of them Array come into role.(Although there are other options also).

You can create array of any thing int,float ,String and objects of any class.

here is the syntax:-

<Classname> <variableName>[]=new <ClassName>[No of objects to be created];

Now For Random class you can do something like this.

Random ranList[]=new Random[10];

By writing this line you have just created a array of Random but not yet instantiated the object using new Keyword.

for that you have to do something like this.

for(int i = 0; i < 10; i++)
{
    Random ranList[i] = new Random();
}

now you have 10 object of Random type each store at a different index of ranList variable.

Hope it helped you!

2 Comments

Thank you for helping me, the array will do it. My other problem is I don't know how many object I need. Maybe 1, maybe 100. So if i create a Random rands[] = new Random[100] then every random will be loaded and using the RAM or no?
@GergőGutyina Yes that is the problem with array,you need to know the size before hand but in your case you can use ArrayList class .

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.