java - how to match the index value in LinkedList -
so part of code containing linkedlist. how can find match in set of numbers.
linkedlist <integer> mylist = new linkedlist<> (); for(int : 1; i<=5; i++){ system.out.println("process " + + has :); int numinput = scan.nextint(); mylist.add(numinput); }
my desired output is:
process 1 has : 3 process 2 has : 4 process 3 has : 1 process 4 has : 5 process 5 has : 2 matched : process 1 , process 3.
you have compare pairs of list items. if index of 1 item equals value of 1 , vice versa, have match. keep in mind, java indices start 0, indices start 1.
public static void main(string[] args) { scanner scan = new scanner(system.in); linkedlist <integer> mylist = new linkedlist<> (); for(int = 1; i<=5; i++){ system.out.print("process " + + " has: "); int numinput = scan.nextint(); mylist.add(numinput); } for(int = 0; < mylist.size(); i++) { for(int j = + 1; j < mylist.size(); j++) { int value1 = mylist.get(i); int value2 = mylist.get(j); if(value1 == (j + 1) && value2 == (i + 1)) { system.out.println("matched : process " + (i + 1) + " , process " + (j + 1) + "."); } } } }
Comments
Post a Comment