Posts
Showing posts with the label os notes
Popular Posts
java:17: error: local variables referenced from a lambda expression must be final or effectively final count ++ ;
Modifying variables inside java lambdas. Integer list[] = {1,2,3,4,5,6,7,8}; int index = 0; Arrays.asList(list).forEach(e -> { System.out.println(e+" at index "+index); index++; }); Error : maze2.java:22: error: local variables referenced from a lambda expression must be final or effectively final System.out.println(e+" at index "+index); ^ maze2.java:23: error: local variables referenced from a lambda expression must be final or effectively final index++; ^ Solution : Integer list[] = {1,2,3,4,5,6,7,8}; int[] index = {0}; Arrays.asList(list).forEach(e -> { System.out.println(e+" at index "+index[0]); index[0]++; }); Output : 1 at index 0 2 at index 1 3 at index 2 4 at index 3 5 at index 4 6 at index 5 7 at index 6 8 at index 7
Binary Tree
My Program to create Binary Tree and print sorted output class Node { int data ; Node left,right; Node(int value) { this.data = value; left = null; right = null; } } class myTree { Node root ; myTree () { root = null ; } // apending to tree void add(int value) { if(root==null) { root = new Node(value); System.out.println("Root created = "+root.data); return; } _add(root,value); } void _add(Node pointer, int value) { if(pointer.data < value) { if(pointer.right != null) { _add(pointer.right , value); } else { pointer.right = new Node(value); System.out.println("Inserted "+pointer.right.data+" to right of "...