English 中文(简体)
不同方法的变量(Java)
原标题:Accessing variable from different methods (Java)
  • 时间:2012-04-09 22:50:38
  •  标签:
  • java

提出的另一个问题。 我正在从事一项任务,以创建洗衣机功能和所有麻风机,我已经陷入一个小问题。

Line 35:21, 原文为rrpos += prearrpo & __,

在我头部工作时...... 试图做到的是从<代码>HashTable(<>/code>方法>上进入arr.length。 我先读一下,有人建议,必须从表面上 cr起大小物体;然而,我认为,这似乎过于复杂。

是否有另一种方法,即能够使用哈希特方法中的变量,但在插入方法中?

Another not so important question involves the giant block of if() statements in the letter(char c) class; im certain there must be a shorter way of doing this... I would have initially used the ascii values; but the specifications were quite particular about using the values 1-26 for lower/upper case letters-

成就

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() 是构造者。 您的<代码>arr[实际上应当成为您的班级的私人成员,而且你应当在所有建筑商中启动这一编号,或者确保你在不附带条件的情况下永远不能进入。

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 :

arr = new String[whatever_size];




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签