索引查找Java实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了索引查找Java实现相关的知识,希望对你有一定的参考价值。

package 索引查找;

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.length;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;
	}

	//向主表中插入元素,从stu这个数组中我们可以看出来里面有0的值,这些值就是预留的空间
	/**
	 * 我们只需要把这些0的值替换掉就行了,值涉及到了索引表和相应的字表,而不是整个表
	 * 所以比较和移动的效率比较高
	 */
	
	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;
		}
		
		//保存关键字到主表中
		stu[start+length] = key;
		indexTable[i].length++;
		return 0;
	}
	
	
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		System.out.println("原始数据:");
		for (int i = 0;i<stu.length;i++) {
			System.out.print(stu[i]+" ");
		}
		System.out.println();
//		System.out.println("输入查找的关键字");
//		int key = input.nextInt();
//		int pos = indexSearch(key);
//		if (pos >= 0) {
//			System.out.printf("查找成功,该关键字位于数组的第%d个位置\n",pos);
//		}else{
//			System.out.println("查找失败!");
//		}
		
		System.out.println("输入插入的关键字");
		int num = input.nextInt();
		if (insertNode(num) == -1) {
			System.out.println("插入失败!");
		}else {
			for (int i = 0;i<stu.length;i++) {
				System.out.print(stu[i]+" ");
			}
			System.out.println();
		}
		
		
		
		
	}
	
	
	
	
}

 

package 索引查找; 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.length;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("原始数据:"); for (int i = 0;i<stu.length;i++) { System.out.print(stu[i]+" "); } System.out.println(); System.out.println("输入查找的关键字"); int key = input.nextInt(); int pos = indexSearch(key); if (pos >= 0) { System.out.printf("查找成功,该关键字位于数组的第%d个位置\n",pos); }else{ System.out.println("查找失败!"); } } }

 

以上是关于索引查找Java实现的主要内容,如果未能解决你的问题,请参考以下文章

索引查找Java实现

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例

片段(Java) | 机试题+算法思路+考点+代码解析 2023

java.lang.IllegalStateException:键 f1 的片段不再存在:索引 3

从搜索文档中查找最小片段的算法?

华为OD机试真题Java实现字符串筛选排序真题+解题思路+代码(2022&2023)