Java 中固定大小数组的替代方案?

发布于 2024-10-13 03:09:43 字数 1329 浏览 8 评论 0原文

[上下文:Java 新手,4 个月最高; C++ 老手。]

我正在开发一个库,该库在很多地方都需要固定大小的数组(“固定字符串”)。我正在尝试使用依赖注入(qv)来解决这个特定问题,所以我想要以下形式的东西:

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

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

需要简单 - 这将进入自动生成的通信协议。我对协议和数据库的控制权为零;我将在最终代码中包含数百个(如果不是数千个)这样的实例。那么,考虑到所有这些:

是 C++ 的唯一替代方案吗

char injectedFixedString[5];

创建自定义类 ?比如:

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];
    }
}

谢谢。 (如果代码太多,我很抱歉)。

[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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南街九尾狐 2024-10-20 03:09:44

确实没有办法用静态保证长度来实现你想要的,除非你准备为每个长度创建一个新类:FixedBarString4、FixedBarString5 等。(可以创建一个类对于使用泛型对长度进行编码的单链表,但我怀疑您是否愿意在现实世界中使用它。

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.

青柠芒果 2024-10-20 03:09:44

我不是 100% 确定你想要实现什么。防止使用不同大小的数组调用构造函数?在编译时?为什么运行时检查和抛出不是可接受的解决方案?

如果您的依赖项注入框架支持新的 bean 验证 标准,那么这将使您在注射期间进行检查:

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

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)
谁许谁一生繁华 2024-10-20 03:09:44

难道你不能在java程序中使用java.lang.String,并且仅在需要时才将其转换为数组吗?使用 toCharArray() 并限制之前的字符串(例如,使用来自 commons-lang 的 StringUtils.right(str, 5) )

或者您可以抛出一个 IllegalArgumentException 当长度超过 5 的字符串传递给该方法时。

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.

-小熊_ 2024-10-20 03:09:44

您可以使用

Foo(Bar injectedBar, char[] injectedFixedString)

它并调用它

new Foo(injectedBar, new char[5])

,或者

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

还值得注意的是 char 是 2 个字节,而不是一个。您的协议采用 16 位字符还是 8 位字符。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文