This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to combine two arrays within a loop?  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having troubles combing both arrays to execute within the loop. I have confused myself way to long with this simple program. I am supposed to write a program that reads 10 numbers and displays the distinct numbers. Example layout of the output is:

Enter an integer: 1
Enter an integer: 3
Enter an integer: 2
Enter an integer: 1
Enter an integer: 5
Enter an integer: 4
Enter an integer: 5
Enter an integer: 2
Enter an integer: 4
Enter an integer: 5
The number of distinct values is 5
1 3 2 5 4

My code is:
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

use Set to display distinct values. I guess you want to display them in the order they were entered - LinkedHashSet is ideal to do this.
Look at this example:
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ireneusz Kordal wrote:Hi,

for( Integer i: set){
System.out.println( i );
}



Or just use
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!