使用线性探测和随机数进行哈希 Table
Hash Table with Linear Probing and Random Numbers
您好,我的任务是显示并计算将随机生成的密钥逐个插入到最初为空的 101 桶散列中所需的操作痕迹 table。
我创建了这个 class 来生成 100 个随机数,然后将它们插入哈希 table class,但我不知道如何将它们插入哈希 table 而我手动完成。
如果您能给我一些建议,我将不胜感激。
代码:
import java.util.Random;
public class RandomNumbers {
static void main(String[] args) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public RandomNumbers() {
for (int i = 0; i < 100; i++) {
System.out.println(getRandomNumberInRange(0, 15024267));
}
}
public static int getRandomNumberInRange(int min, int max) {
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
String[] numbersPrinting;
}
如您所见,我手动插入了随机数。
哈希Table代码:
import java.util.Arrays;
/**
*
*
*/
public class HashFunction {
String[] theArray;
int arraySize;
int itemsInArray = 0;
int K = 0;
/**
*
*/
public static void main(String[] args) {
HashFunction theFunc = new HashFunction(101);
String[] elementsToAdd2 = {"3638273","3483793","1362909","14908691","14624805","3959017","14208240","8911589","3701879", };
theFunc.hashFunction2(elementsToAdd2, theFunc.theArray);
}
public void hashFunction2(String[] stringsForArray, String[] theArray) {
for (int n = 0; n < stringsForArray.length; n++) {
String newElementVal = stringsForArray[n];
// Create an index to store the value in by taking
// the modulus
int arrayIndex = Integer.parseInt(newElementVal) % 101;
System.out.println("P" + arrayIndex + " " + "I" + newElementVal + "@" + arrayIndex );
// Cycle through the array until we find an empty space
while (theArray[arrayIndex] != "-1") {
++arrayIndex;
System.out.println( "P" + arrayIndex);
// If we get to the end of the bucket go back to index 0
arrayIndex %= arraySize;
}
theArray[arrayIndex] = newElementVal;
}
}
HashFunction(int size) {
arraySize = size;
theArray = new String[size];
Arrays.fill(theArray, "-1");
}
}
不知道如何把随机数class中的随机数插入HashTableclass
我认为你应该做的是在 HashFunction
class:
的主函数中创建随机数
public static void main(String[] args) {
HashFunction theFunc = new HashFunction(101);
String[] elementsToAdd2 = new String[101];
for (int i = 0; i <= 100; i++) {
elementsToAdd2[i] = Integer.toString(RandomNumbers.getRandomNumberInRange(0, 15024267));
}
theFunc.hashFunction2(elementsToAdd2, theFunc.theArray);
}
通过这种方式,您将可以访问 HashFunction
class 中的随机数数组,并且可以将其添加到 HashFunction class 的 theArray
数组中。
您好,我的任务是显示并计算将随机生成的密钥逐个插入到最初为空的 101 桶散列中所需的操作痕迹 table。
我创建了这个 class 来生成 100 个随机数,然后将它们插入哈希 table class,但我不知道如何将它们插入哈希 table 而我手动完成。
如果您能给我一些建议,我将不胜感激。
代码:
import java.util.Random;
public class RandomNumbers {
static void main(String[] args) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public RandomNumbers() {
for (int i = 0; i < 100; i++) {
System.out.println(getRandomNumberInRange(0, 15024267));
}
}
public static int getRandomNumberInRange(int min, int max) {
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
String[] numbersPrinting;
}
如您所见,我手动插入了随机数。
哈希Table代码:
import java.util.Arrays;
/**
*
*
*/
public class HashFunction {
String[] theArray;
int arraySize;
int itemsInArray = 0;
int K = 0;
/**
*
*/
public static void main(String[] args) {
HashFunction theFunc = new HashFunction(101);
String[] elementsToAdd2 = {"3638273","3483793","1362909","14908691","14624805","3959017","14208240","8911589","3701879", };
theFunc.hashFunction2(elementsToAdd2, theFunc.theArray);
}
public void hashFunction2(String[] stringsForArray, String[] theArray) {
for (int n = 0; n < stringsForArray.length; n++) {
String newElementVal = stringsForArray[n];
// Create an index to store the value in by taking
// the modulus
int arrayIndex = Integer.parseInt(newElementVal) % 101;
System.out.println("P" + arrayIndex + " " + "I" + newElementVal + "@" + arrayIndex );
// Cycle through the array until we find an empty space
while (theArray[arrayIndex] != "-1") {
++arrayIndex;
System.out.println( "P" + arrayIndex);
// If we get to the end of the bucket go back to index 0
arrayIndex %= arraySize;
}
theArray[arrayIndex] = newElementVal;
}
}
HashFunction(int size) {
arraySize = size;
theArray = new String[size];
Arrays.fill(theArray, "-1");
}
}
不知道如何把随机数class中的随机数插入HashTableclass
我认为你应该做的是在 HashFunction
class:
public static void main(String[] args) {
HashFunction theFunc = new HashFunction(101);
String[] elementsToAdd2 = new String[101];
for (int i = 0; i <= 100; i++) {
elementsToAdd2[i] = Integer.toString(RandomNumbers.getRandomNumberInRange(0, 15024267));
}
theFunc.hashFunction2(elementsToAdd2, theFunc.theArray);
}
通过这种方式,您将可以访问 HashFunction
class 中的随机数数组,并且可以将其添加到 HashFunction class 的 theArray
数组中。