Package index lookup; import Java.util.scanner;public class Indexsearch {public static long stu[] = { 1080101,1080102,1080103,1080104,1080105,1080106,0,0,0,0,1080201,1080202,1080203,1080204,0,0,0,0,0,0,1080301,1080302,10803 03,1080304,0,0,0,0,0,0};static item[]indextable = {New Item (10801,0,6), new Item (10802,10,4), new Item (10803,20,4)}; public static int Indexsearch (int key) {int I,index1,start = 0,length = 0;index1 = key/100;for (i = 0;i<indextable.lengt h;i++) {if (Indextable[i].index = = index1) {start = Indextable[i].start;length = Indextable[i].length;break;}} if (i >= indextable.length) {return-1;} for (i = start;i<start+length;i++) {if (stu[i] = = key) {return i;}} return-1;} Inserting elements into the main table, from the STU array we can see that there are 0 of the values, these values are reserved space/** * We just need to replace these 0 values, the value of the index table and the corresponding Word table, not the entire table * so the comparison and movement efficiency is relatively high */public static int insertnode (int key) {int I,index1,start = 0,length = 0;index1 = key/100;for (i = 0;i<indextable.length;i++) { if (Indextable[i].index = = index1) {start = Indextable[i].start;length = IndextaBle[i].length;break;}} if (i >= indextable.length) {return-1;} Save keyword to Main table stu[start+length] = Key;indextable[i].length++;return 0;} public static void Main (string[] args) {Scanner input = new Scanner (system.in); SYSTEM.OUT.PRINTLN ("raw Data:"); for (int i = 0;i<stu.length;i++) {System.out.print (stu[i]+ "");} System.out.println ();//system.out.println ("Input lookup keyword"),//int key = Input.nextint ();//int pos = Indexsearch (key);//if ( POS >= 0) {//system.out.printf ("lookup succeeded, this keyword is located in the array of%d locations \ n", POS);//}else{//system.out.println ("Lookup failed! //}SYSTEM.OUT.PRINTLN ("Input inserted keyword"), int num = Input.nextint (), if (Insertnode (num) = =-1) {System.out.println ("Insert failed! ");} else {for (int i = 0;i<stu.length;i++) {System.out.print (stu[i]+ "");} System.out.println ();}}}
Package index Lookup; Import Java.util.Scanner; public class Indexsearch {public static long stu[] = {1080101,1080102,1080103,1080104,1080105,1080106,0,0,0,0, 1080201, 1080202,1080203,1080204,0,0,0,0,0,0, 1080301,1080302,1080303,1080304,0,0,0,0,0,0}; Static item[]indextable = {New Item (10801,0,6), new Item (10802,10,4), new Item (10803,20,4)}; public static int Indexsearch (int key) {int I,index1,start = 0,length = 0; index1 = key/100; for (i = 0;i<indextable.le ngth;i++) {if (Indextable[i].index = = index1) {start = Indextable[i].start; length = indextable[i].length; break;}} if (I >= indextable.length) {return-1;} for (i = start;i<start+length;i++) {if (stu[i] = = key) {return i;}} return-1; public static void Main (string[] args) {Scanner input = new Scanner (system.in); SYSTEM.OUT.PRINTLN ("raw Data:"); for (int i = 0;i<stu.length;i++) {System.out.print (stu[i]+ "");} System.out.println (); SYSTEM.OUT.PRINTLN ("Enter keywords to find"); int key = Input.nextint (); int pos = Indexsearch (kEY); if (pos >= 0) {System.out.printf ("lookup succeeded, the keyword is located in the array of%d locations \ n", POS);} else{System.out.println ("Find failed! "); } } }
Index Lookup Java Implementation