简体   繁体   English

从不同的方法访问变量 (Java)

[英]Accessing variable from different methods (Java)

another question to put out there.另一个问题要提出来。 I was working on an assignment to create hash functions and all that jazz, and i have stumbled across a small problem.我正在完成一项创建 hash 函数和所有爵士乐的任务,我偶然发现了一个小问题。

Line 35:21, where it reads arrpos += prearrpo & ______,

in my head works... What im trying to do is access arr.length from the HashTable() method.在我的脑海里工作......我想做的是从HashTable()方法访问arr.length I've read around, suggestions with needing to creat an object of size arr.length;我已经阅读了关于需要创建大小为 arr.length 的 object 的建议; however in my mind, this seems overly complicated-然而在我看来,这似乎过于复杂了——

Is there another way i can access the variable in the HashTable method, but inside the insert method?有没有另一种方法可以在 HashTable 方法中访问变量,但在插入方法中?

Another not so important question involves the giant block of if() statements in the letter( char c ) class;另一个不太重要的问题涉及字母 ( char c ) class 中的巨大if()语句块; im certain there must be a shorter way of doing this... I would have initially used the ascii values;我确定必须有更短的方法来做到这一点……我最初会使用 ascii 值; but the specifications were quite particular about using the values 1-26 for lower/upper case letters-但是规范对于使用值 1-26 来表示小写/大写字母非常特别-

Thanks谢谢

import java.io.*;

public class HashTable {

    public HashTable() {
        //Create an array of size 101
        String arr[] = new String[101];
        //System.out.println("Size1: ");
    }

    public HashTable(int tsize) {
        int size = 2 * tsize;
        //System.out.println("Size: " + size);
        boolean isPrime = checkPrime(size);
        //System.out.println("IsPrime: " + isPrime);
        while (isPrime == false) {
            //System.out.println("Size: " + size);
            size++;
            isPrime = checkPrime(size);
        }
        //System.out.println("Size: " + size);
        String arr[] = new String[size];
    }

    public boolean insert(String line) {

        String str = line;
        char[] ch = str.toCharArray();
        int slen = str.length();
        int arrpos = 0;
        int hash = slen;
        for (int i = 0; i < slen; i++) {
            double prearrpo = letter(ch[i]) * Math.pow(32, (hash - 1));
            arrpos += prearrpo % arr.length();
            hash--;

        }
        System.out.println(arrpos);
        System.out.println("array size:");
        System.out.println();
        return false;

    }

    private int letter(char c) {
        char ch = c;
        if (ch == 'A' || ch == 'a') {
            return 1;
        }
        if (ch == 'B' || ch == 'b') {
            return 2;
        }
        if (ch == 'C' || ch == 'c') {
            return 3;
        }
        if (ch == 'D' || ch == 'd') {
            return 4;
        }
        if (ch == 'E' || ch == 'e') {
            return 5;
        }
        if (ch == 'F' || ch == 'f') {
            return 6;
        }
        if (ch == 'G' || ch == 'g') {
            return 7;
        }
        if (ch == 'H' || ch == 'h') {
            return 8;
        }
        if (ch == 'I' || ch == 'i') {
            return 9;
        }
        if (ch == 'J' || ch == 'j') {
            return 10;
        }
        if (ch == 'K' || ch == 'k') {
            return 11;
        }
        if (ch == 'L' || ch == 'l') {
            return 12;
        }
        if (ch == 'M' || ch == 'm') {
            return 13;
        }
        if (ch == 'N' || ch == 'n') {
            return 14;
        }
        if (ch == 'O' || ch == 'o') {
            return 15;
        }
        if (ch == 'P' || ch == 'p') {
            return 16;
        }
        if (ch == 'Q' || ch == 'q') {
            return 17;
        }
        if (ch == 'R' || ch == 'r') {
            return 18;
        }
        if (ch == 'S' || ch == 's') {
            return 19;
        }
        if (ch == 'T' || ch == 't') {
            return 20;
        }
        if (ch == 'U' || ch == 'u') {
            return 21;
        }
        if (ch == 'V' || ch == 'v') {
            return 22;
        }
        if (ch == 'W' || ch == 'w') {
            return 23;
        }
        if (ch == 'X' || ch == 'x') {
            return 24;
        }
        if (ch == 'Y' || ch == 'y') {
            return 25;
        }
        if (ch == 'Z' || ch == 'z') {
            return 26;
        }
        return 0;
    }

    public boolean lookUp(String string) {
        // 
        return false;
    }

    public String getNum() {
        // 
        return null;
    }

    public int length() {

        return 0;
    }

    private static boolean checkPrime(int size) {

        if (size % 2 == 0) {
            return false;
        }
        double c = Math.sqrt(size);
        for (int i = 3; i < c; i += 2) {
            if (size % i == 0) {
                return false;
            }
        }



        return true;
    }
}

public HashTable() is a constructor. public HashTable()是一个构造函数。 Your arr[] should actually be a private member of your class and you should initialize it in all constructors or make sure you never access without intializing it.你的arr[]实际上应该是你的 class 的私有成员,你应该在所有构造函数中初始化它,或者确保你永远不会在没有初始化它的情况下访问它。

public class HashTable {

    private String[] arr;

    public HashTable() 
    {
        //Create an array of size 101
        arr[] = new String[101];
        System.out.println("Size1: ");
    }
etc...

Since it seems that your implementation is array backed, you need to declare the array as a member variable:由于您的实现似乎是支持数组的,因此您需要将数组声明为成员变量:

public class HashTable {
 String arr[] = null;

And then initialize it in your constructors ( note in Java world methods like HashTable() is called a constructor) as:然后在你的构造函数中初始化它(注意在 Java 世界中像HashTable()这样的方法被称为构造函数)如下:

arr = new String[whatever_size];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM