Monday, 27 November 2017

Java Program to Perform Searching Based on Locality of Reference


Code:

package com.executecodes.combinatorial;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class LocalityBasedSearching
{
    public static void main(String[] args)
    {
        List items = new LinkedList();
        Integer n = 10;
        Scanner sc = new Scanner(System.in);
        Random rand = new Random();
        while (n > 0)
        {
            items.add(rand.nextInt(100));
            n--;
        }
        System.out.println(items.toString());
        boolean flag = true;
        boolean found = false;
        Integer numberofInstance;
        while (flag == true)
        {
            numberofInstance = 0;
            System.out.println("Enter the element to find: ");
            Integer search = sc.nextInt();
            for (int i = 0; i < items.size(); i++)
            {
                if (items.get(i).equals(search))
                {
                    found = true;
                    System.out.println("Element found at index " + i
                            + "\nReordering list...");
                    // Integer temp = items.get(numberofInstance);
                    // items.set(numberofInstance, search);
                    items.add(numberofInstance, search);
                    items.remove(i + 1);
                    // items.set(i, temp);
                    System.out.println("Reordered list: " + items.toString());
                    numberofInstance++;
                    // break;
                }
            }
            if (found == false)
            {
                System.out.println("No such element found.");
            }
            System.out.println("Do you want to continue? /");
            flag = sc.nextBoolean();
        }
        sc.close();
    }
}



Output:

[52, 94, 58, 8, 78, 0, 30, 81, 16, 58]
Enter the element to find: 
8
Element found at index 3
Reordering list...
Reordered list: [8, 52, 94, 58, 78, 0, 30, 81, 16, 58]
Do you want to continue? /
true
Enter the element to find: 
58
Element found at index 3
Reordering list...
Reordered list: [58, 8, 52, 94, 78, 0, 30, 81, 16, 58]
Element found at index 9
Reordering list...
Reordered list: [58, 58, 8, 52, 94, 78, 0, 30, 81, 16]
Do you want to continue? /
true
Enter the element to find: 
94
Element found at index 4
Reordering list...
Reordered list: [94, 58, 58, 8, 52, 78, 0, 30, 81, 16]
Do you want to continue? /
false


More Java Programs:

















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...