开发者

Alternative to Fixed-Size Arrays in Java?

[Context: new to Java, 4 months tops; old hand at C++.]

I'm working on a library that requires an array of a fixed size ("fixed string") in many places. I'm trying to use Dependency Injection (q.v.) for this particular problem, so I would like something of the form:

class Foo 
{
    private Bar injectedBar;
    private char[] injectedFixedString;

    Foo(Bar injectedBar, /* what can go here? */ char[5] injectedFixedString);
    { /* initializing code goes here /* }
}

Simple is required -- this is going into an auto-generated communications protocol. I have zero control of the protocol and database it's being derived from; I will have hundreds, if not thousands, of these instances in the final code. So, given all that:

Is my only alternative to the C++:

char injectedFixedString[5];

to create a custom class? Something like:

开发者_如何转开发
class FixedBarString {
    /* could also set this in the constructor, but this complicates code generation a tad */
    public static integer STRING_SIZE = 5; /* string size */
    char[] fixedString = new char[STRING_SIZE];

    FixedBarString(char[] string) throws RuntimeException {
      /* check the string here; throw an exception if it's the wrong size.
         I don't like constructors that throw however. */
    }

    public void setString(char[] string) throws RuntimeException {
      /* check the string here */
    }

    public char[] getString() {
      /* this isn't actually safe, aka immutable, without returning clone */
    }

    public char[] createBlankString() {
      return new char[STRING_SIZE];
    }
}

Thanks. (My apologies if this is too much code).


There is indeed no way to do what you want with a statically guaranteed length, unless you are prepared to have a new class for every length: FixedBarString4, FixedBarString5, etc. (It is possible to create a class for single linked list of chars which uses generics to encode the length. But I doubt you would like to use that in your real world scenario.


I'm not 100% certain what you want to achieve. Prevent the constructor from being called with an array of a different size? At compile time? Why is a check&throw at runtime not an acceptable solution?

If your dependency injection framework supports the new bean validation standard, then this would get you the checking during injection:

Foo(Bar injectedBar, @Size(min=5,max=5) char[] injectedFixedString)


Can't you use java.lang.String in the java program, and only convert it to an array when/if needed. Using toCharArray(), and limiting the string before that (using StringUtils.right(str, 5) from commons-lang, for example)

Or you can throw an IllegalArgumentException when a string with a length more than 5 is passed to the method.


You can have

Foo(Bar injectedBar, char[] injectedFixedString)

and call it with

new Foo(injectedBar, new char[5])

or

Foo(Bar injectedBar, fixedStringLength)
new Foo(injectedBar, 5)

its also worth noting that char is 2 bytes, not one. does your protocol assume 16-bit characters or 8-bit.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜