• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help creating and sorting through arrays  RSS feed

 
Clemz Chins
Greenhorn
Posts: 10
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I am still learning Java and one of my assignments requires me to create two arrays (of the same size) and populate them with random integers. I am to sort them using Selection Sort or Insertion and take note of the duration of this sort. The following is what I have come up with thus this far and am wishing to get some input from the gurus here as I am encountering an error in the code.

Thank you very much,

Here's my code:
 
Liutauras Vilda
Sheriff
Posts: 4926
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problems at least are with line 13, what you think might be wrong?

Also, please always add error message when you say you get an error and seeking for a help to resolve that.
 
Liutauras Vilda
Sheriff
Posts: 4926
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at line 12 too, how many iterations your loop would run if the code would compile.
 
Clemz Chins
Greenhorn
Posts: 10
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, the error is on line 13 I believe it says:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error on token ".", Identifier expected after this token

at Measuringarrays.main(Measuringarrays.java:13)
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I know that is what all the books tell you about populating an array, but for over three years there has been a much better way to do it.
I don't like doing arithmetic with Math#random. Look here and the two links in that post for an explanation. So for a long time, the best way to create such “random” numbers was with the Random#nextInt method. But since 2014, there has been an even better way to create such an array.
The Random class has an overloaded method called ints() (one version here), and you can get that to create an IntStream containing exactly how many numbers you want, between the lower bound and less than the upper bound. The the IntStream object has a method called toArray(), which returns exactly what you want.That will create a 123456789‑element array containing ints, between −123 and 455.

You now don't have to worry about the size of your array or whether your loop will match its size or anything. You just have to remember that you won't find 456 in it.
 
Clemz Chins
Greenhorn
Posts: 10
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie, much thanks to you. I also wish to thank you Liutauras Vilda. The pointers really helped, now am back at it, panel beating it into shape. Let's see how it pans out.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to time your sort then the population of the random array should be identical each time for consistent results. You can achieve this by seeding the random number generator:
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your variable names should be meaningful so as to guide the reader through the code.
"merRy10" doesn't mean anything, at least, not in English. You also have "merRy1" and "merRy2", obviously these are not all of the same type so the names should reflect that in some way.

Not really a convention but the most popular name for a Random object is "rand".
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!