开发者

have a char reference an object in an array

开发者 https://www.devze.com 2023-02-14 01:36 出处:网络
Is it possible to have a char point to an object in an array? im trying to have the characters : +,-,*,/point to an index in my array.

Is it possible to have a char point to an object in an array? im trying to have the characters : +,-,*,/ point to an index in my array.

I AM WELL AWARE MY SECTION BELOW IS NOT CORRECT SYNTAX. its just my way of describing what i wish to accomplish.

 public static void main(String[] args) {

            Operations plus;
            Operations minus;
            Operati开发者_如何学Pythonons multiply;
            Operations divide;
    /**********Create jumpTable*******************/
            Operations[] jumpTable = new Operations[255];
    /**********Add Object to jumpTable************/
            jumpTable[0] = new Addition();
            jumpTable[1] = new Subtraction();
            jumpTable[2] = new Multiplication();
            jumpTable[3] = new Division();
    /**********Point to index in table************/
            plus = jumpTable[0];
            minus = jumpTable[1];
            multiply = jumpTable[2];
            divide = jumpTable[3];

    //this is what im trying to do:
    //***************************************
     char +;
     char -;
     '+' = plus
     '-' = minus and etc...
   //****************************************
           double x = Double.parseDouble(args[0]);
           double y = Double.parseDouble(args[1]);


        System.out.printf("%f %s %f = %f%n", x, op, y, op.Compute(x, y));

        }
    }


Is it possible to have a char point to an object in an array?

Assuming that you are asking: "is it possible to use a char as an index for an array", then the answer is "Yes it is possible".

An index expression for an array can have any type that can be promoted to int; see JLS 15.13. And the char type can be promoted to int. (You don't even need to include a typecast to make it happen. It just works.)


Could you use a Map<String, Operations> instead of the Operations[]?

Also Operations should probably be called Operation.


you can cast the char to a int or byte

char addition = '+';
operators[(byte)addition] = ...


You can use a value of type char every where a value of type int is allowed, and (apart from String concatenation and where there are two methods of both types) it works the same as the int obtained by casting.

So, operators['+'] is identical to operators[43], and similar.

If you only need *, +, -, /, you could use an array of length 6, and index into it by taking the difference to * (which is the first of them):

Operations[] jumptable = {
   new Multiplication(),  // * = 42 = '*' + 0
   new Addition(),        // + = 43 = '*' + 1
   null,                  // , = 44 
   new Subtraction(),     // - = 45 = '*' + 3
   null,                  // . = 46
   new Division()         // / = 47 = '*' + 5
};

char operator = ...;

Operation op = jumptable[operator - '*'];

Of course you could always make the table as big as necessary and directly index.


No.

A char is a Java primitive type representing a single 16-bit unicode character.

See here:

http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

0

精彩评论

暂无评论...
验证码 换一张
取 消