Java 中固定大小数组的替代方案?
[上下文: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
确实没有办法用静态保证长度来实现你想要的,除非你准备为每个长度创建一个新类: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.
我不是 100% 确定你想要实现什么。防止使用不同大小的数组调用构造函数?在编译时?为什么运行时检查和抛出不是可接受的解决方案?
如果您的依赖项注入框架支持新的 bean 验证 标准,那么这将使您在注射期间进行检查:
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:
难道你不能在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. UsingtoCharArray()
, and limiting the string before that (usingStringUtils.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.您可以使用
它并调用它
,或者
还值得注意的是 char 是 2 个字节,而不是一个。您的协议采用 16 位字符还是 8 位字符。
You can have
and call it with
or
its also worth noting that char is 2 bytes, not one. does your protocol assume 16-bit characters or 8-bit.