Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Sorting an array

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone new to java and have been trying to get my to get array to do two things. Those are 1. Sort using the sort method and 2. Only generate random numbers between 1-100. I have looked through the Oracle site on the sort method but I do not think I understand it. Thanks in advanced.




 
Bartender
Posts: 4107
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Hector,

welcome to the Ranch and enjoy the stay!

There's nothing wrong with your sorting, but to see the effect, it might be wise to fill the array first with random numbers, and then do the sorting.
 
Hector Jaimes
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this mean that I would require another for loop maybe?

Piet Souris wrote:hi Hector,

welcome to the Ranch and enjoy the stay!

There's nothing wrong with your sorting, but to see the effect, it might be wise to fill the array first with random numbers, and then do the sorting.



 
Piet Souris
Bartender
Posts: 4107
156
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you already have the required loop that fills the array. Just do Arrays.sort after that loop.

By the way: to print an array, use System.out.println(Arrays.toString(array));
 
Hector Jaimes
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it and it works. Thank you!
 
Piet Souris
Bartender
Posts: 4107
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
Saloon Keeper
Posts: 7392
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI rand.nextInt(100) will generate ints in the range of 0-99. If you want 1-100 use rand.nextInt(100)+1.
 
Marshal
Posts: 70674
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer to use an [Int]Stream:-The Random#ints() method creates an IntStream. In this instance that IntStream processes 100L (it's a long) int numbers minimum 1 and maximum < 101. (Range contains 101 − 1 = 100 numbers.) The toArray() method simply collects all the numbers processed as an array. You can of course interject another method call, which replaces the first IntStream by a second one containing the numbers in a particular order (line 2):-Note I am using different indentation with the dot operators aligned. This is another way to do it:-...or even...If any of that last code looks familiar it is because Arrays#stream() is another way to create an IntStream. You now have the original and the sorted version.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic