-List of names in a database
-names have double assigned to them, x
-each name has random 10 digit double generated from 0 to x
-the name with the highest number double is assigned to a variable
-if two names generate the same double then the name with in the list is assigned to the variable
-once a name is assigned to the variable, it falls out of queue and cannot be assigned again for another 24 hours

What components of Java do I need to be able to understand to know how to do this?

Or if you have pre-written code with similar functionality that would be helpful too.

Thanks tons and much love

Recommended Answers

All 9 Replies

import java.util.Random;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public final class RandomGaussian {
  
  public static final void main(String... aArgs){
	  Random randomGenerator = new Random();
	  List<Integer> level = new ArrayList<Integer>();
	  level.add(37);
      level.add(2);
      level.add(2);
      level.add(4);
      int position = 0;
      int listsize = level.size();
      while(position <= listsize){
      
      int levelDouble = randomGenerator.nextInt(level.get(position));
      log("Generated : " + levelDouble);
      position = position + 1;
      }
      
      
    
    
    log("Done.");
  }
  
  private static void log(String aMessage){
    System.out.println(aMessage);
  }
}

Why does this give me an array out of bounds error?

On line 16:

int listsize = level.size()-1;

The last element in an array is always the length(or size in this case) - 1. Your while loop is running one time too many.

Member Avatar for ztini

Drop listsize all together imo.

while(position < level.size()){

Never store variables that can be replicated from a method call of O(1) efficiency.

This can’t be a work or real life project. Is this a school assignment and you trying to get the shortcut?

not for school, for pleasure. I think coding is fun and want to learn it. If anyone has anything to contribute to my original question please let me know. thanks

Never store variables that can be replicated from a method call of O(1) efficiency.

Explain please.

@ oldezwe

if you done with your :

-List of names in a database
-names have double assigned to them, x

then sent here your Sql dump with column only, without those is your pleasure ..., so far

much luck

Member Avatar for ztini

Explain please.

If I have this code:

int a = 10;
int b = 5;
int c = a + b;
System.out.println(c);

You do not want to use the int c. Your code should model this:

int a = 10;
int b = 5;
System.out.println(a + b);

There are 2 fold reasons for this. 1) less memory is used, and 2) less complexity and less tracing of data during debugging.

The catch is that you only do not want to store methods that are O(1) efficiency. So, if I have have something like this:

public int runLoop(int value) {
for (int i = 0; i < 10; i++)
value += value;
}

...

and now I want to print it several times:

System.out.println(runLoop(5));
System.out.println(runLoop(5));
System.out.println(runLoop(5));

Every time I invoke the method runLoop(), it runs the loop n times (or 10 in this case). This method is of O(n) efficiency. To display the result for the 3 printlns I have to loop 30 times!

However, if I do:

public int runLoop(int value) {
for (int i = 0; i < 10; i++)
value += value;
}

...

int a = runLoop(5);

System.out.println(a);
System.out.println(a);
System.out.println(a);

The O(n) loop is only called once and only 10 iterations are used. This is clearly more efficient than before. So, if you were going to use runLoop(5) more than once, you would obviously want to store its returned value and just that instead of calling the loop every time.

Hopefully that makes sense.

If I have this code:

int a = 10;
int b = 5;
int c = a + b;
System.out.println(c);

You do not want to use the int c. Your code should model this:

int a = 10;
int b = 5;
System.out.println(a + b);

There are 2 fold reasons for this. 1) less memory is used, and 2) less complexity and less tracing of data during debugging.

The catch is that you only do not want to store methods that are O(1) efficiency. So, if I have have something like this:

public int runLoop(int value) {
for (int i = 0; i < 10; i++)
value += value;
}

...

and now I want to print it several times:

System.out.println(runLoop(5));
System.out.println(runLoop(5));
System.out.println(runLoop(5));

Every time I invoke the method runLoop(), it runs the loop n times (or 10 in this case). This method is of O(n) efficiency. To display the result for the 3 printlns I have to loop 30 times!

However, if I do:

public int runLoop(int value) {
for (int i = 0; i < 10; i++)
value += value;
}

...

int a = runLoop(5);

System.out.println(a);
System.out.println(a);
System.out.println(a);

The O(n) loop is only called once and only 10 iterations are used. This is clearly more efficient than before. So, if you were going to use runLoop(5) more than once, you would obviously want to store its returned value and just that instead of calling the loop every time.

Hopefully that makes sense.

just a tip but remember to use code tags around your code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.