Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab10_final;
- public class Adresowanie_kwadratowe {
- private Obiekt[] hashArray;
- private int arraySize;
- public Adresowanie_kwadratowe(int size){
- arraySize = size;
- hashArray = new Obiekt[arraySize];
- }
- public void dump(){
- System.out.print("Tablica: ");
- for(int j=0; j<arraySize; j++){
- if(hashArray[j] != null)
- System.out.print(hashArray[j].getValue()+" ");
- else
- System.out.print("* ");
- }
- System.out.println();
- }
- public int hashFunction(int key){
- return key % arraySize;
- }
- public void put(int key, Obiekt ob){
- if((double)size()/(double)arraySize < 0.75){
- int value = ob.getValue();
- int hashVal = hashFunction(key);
- while(hashArray[hashVal] != null){
- ++hashVal;
- hashVal %= arraySize;
- }
- hashArray[hashVal] = ob;
- }else{
- resize();
- put(key, ob);
- }
- }
- public Obiekt get(int key){
- int hashVal = hashFunction(key);
- while(hashArray[hashVal] != null){
- if(hashArray[hashVal].getValue() == key)
- return hashArray[hashVal];
- ++hashVal;
- hashVal %= arraySize;
- }
- return null;
- }
- public boolean containsKey(int key){
- int hashVal = hashFunction(key);
- while(hashArray[hashVal] != null){
- if(hashArray[hashVal].getValue() == key)
- return true;
- ++hashVal;
- hashVal %= arraySize;
- }
- return false;
- }
- public int size(){
- int size = 0;
- for(int j=0; j<arraySize; j++){
- if(hashArray[j] != null)
- size++;
- }
- return size;
- }
- public boolean isEmpty(){
- boolean isEmpty = true;
- for(int j=0; j<arraySize; j++){
- if(hashArray[j] != null){
- isEmpty = false;
- return isEmpty;
- }
- }
- return isEmpty;
- }
- public void resize(){
- Adresowanie_kwadratowe kw = new Adresowanie_kwadratowe(2*arraySize);
- for(int j=0; j<arraySize; j++){
- if(hashArray[j] != null)
- kw.put(j, hashArray[j]);
- }
- hashArray = kw.hashArray;
- arraySize = kw.hashArray.length;
- }
- public static void main(String[] args){
- Adresowanie_kwadratowe kw = new Adresowanie_kwadratowe(13);
- kw.dump();
- System.out.println("Czy tablica jest pusta? "+ kw.isEmpty());
- System.out.println("Dodano 3.");
- kw.put(3, new Obiekt(3));
- kw.dump();
- System.out.println("Dodano 5, 2, 1, 7, 20.");
- kw.put(5, new Obiekt(5));
- kw.put(2, new Obiekt(2));
- kw.put(1, new Obiekt(1));
- kw.put(7, new Obiekt(7));
- kw.put(20, new Obiekt(20));
- kw.dump();
- System.out.println("Dodano 35, 4, 8, 29, 31, 50, 69.");
- kw.put(35, new Obiekt(35));
- kw.put(4, new Obiekt(4));
- kw.put(8, new Obiekt(8));
- kw.put(29, new Obiekt(29));
- kw.put(31, new Obiekt(31));
- kw.put(50, new Obiekt(50));
- kw.put(69, new Obiekt(69));
- kw.dump();
- System.out.println("Czy tablica jest pusta? "+kw.isEmpty());
- System.out.println("Czy zawiera coś o kluczu 9? " + kw.containsKey(9));
- System.out.println("Czy zawiera coś o kluczu 1? " + kw.containsKey(1));
- System.out.println("Get 35? " + kw.get(35));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment