Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to combine two arrays within a loop?

 
Nick Hallloran
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:
 
Ireneusz Kordal
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:
 
Sridhar Santhanakrishnan
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic