[英]Please suggest me a suitable Java datatype
I have a not so difficult question to ask and i wonder if anyone could help me out.. 我有一个不太难的问题要问,我想知道是否有人可以帮助我。
I have a string object similar to " Hello i am a human being " that i extracted using Tess4J. 我也有类似的字符串对象“ 你好,我是一个人 ”,我用Tess4J提取。 Next im trying to find out if each word in this sentence is bold, italic or underlined
接下来,我试图找出该句子中的每个单词是粗体,斜体还是带下划线
afterwards i require this information to be returned from the method along with the source word. 之后,我要求将此信息与源字一起从方法中返回。 problem is i dont know a suitable datatype to do this operation.
问题是我不知道执行此操作的合适数据类型。
can some one suggest me a good data-structure if possible ? 如果可能的话,有人可以建议我一个好的数据结构吗?
additional points 附加点
This is a sample structure - 这是一个示例结构-
"Hello" , true , false , false
"Hi", false , false , false
"Hi", true, true, false
Any assistance is greatly appreciated, Thank you, Sumal. 非常感谢您的协助,谢谢。
Simply write your own class. 只需编写自己的课程。 Something like this:
像这样:
public class FormattedText {
private String text;
private boolean bold;
private boolean italic;
private boolean underlined;
// setter and getter
}
I'm not sure why comments and answers suggest certain fields . 我不确定为什么评论和答案会建议某些领域 。 Usually, when creating a class, one should rather focus on the interface .
通常,在创建类时,应该宁愿专注于interface 。
Particularly, referring to the point 特别是指点
there is a massive number of words in this
这个词有很多
one should consider that the fields do not matter, and the three boolean
fields can be condensed into a single int
field - roughly like this: 人们应该认为这些字段无关紧要,并且三个
boolean
字段可以压缩为一个int
字段-大致如下所示:
class FormattedText {
private String string;
private int style;
private static final int PLAIN = 0;
private static final int BOLD = 1;
private static final int ITALIC = 2;
private static final int UNDERLINED = 4;
FormattedText(String string, boolean bold, boolean italic, boolean underlined)
{
this.string = string;
if (bold) style |= BOLD;
if (italic) style |= ITALIC;
if (underlined) style |= UNDERLINED;
}
public String getString() {
return string;
}
public boolean isBold() {
return (style & BOLD) != 0;
}
public boolean isItalic() {
return (style & ITALIC) != 0;
}
public boolean isUnderlined() {
return (style & UNDERLINED) != 0;
}
}
One could even "compress" this further. 人们甚至可以进一步“压缩”这一点。 If you really have to handle a massive amount of these entries, and depending on whether/how this information can change, you could consider a dedicated data structure that stores the information about all words in dedicated
BitSet
s, for example. 例如,如果确实需要处理大量这些条目,并且取决于是否可以/如何更改此信息,则可以考虑使用专用的数据结构,该结构将有关所有字的信息存储在专用的
BitSet
。
An alternative custom-class approach which tries to avoid multiple boolean flags by using an EnumSet
(or Guava's Sets.immutableEnumSet()
). 一种替代的自定义类方法,该方法尝试通过使用
EnumSet
(或Guava的Sets.immutableEnumSet()
)来避免多个布尔标志。
public class FormattedText {
private final String text;
private final Set<TextProperty> properties;
public FormattedText(String text, TextProperty... properties) {
this(text, EnumSet.copyOf(Arrays.asList(properties)));
}
public FormattedText(String text, Set<TextProperty> properties) {
this.text = text;
this.properties = properties;
}
// additional constructors, getters, logic
}
public enum TextProperty {
BOLD, ITALIC, UNDERLINED;
}
Used like this: 像这样使用:
new FormattedText("Hello", TextProperty.BOLD);
This way, you only have a single field containing all the necessary properties and methods to act on them. 这样,您只有一个包含所有必要属性和方法的字段。 You can easily add new properties (strikethrough, superscript, etc.?) by amending the
enum
and you won't need a million fields with a million if-else blocks. 您可以通过修改
enum
轻松地添加新属性(删除线,上标等?),并且不需要一百万个带有一百万个if-else块的字段。
Alternatively, you could even do this: 另外,您甚至可以这样做:
public class FormattedText {
private final String text;
private final Set<TextProperty> properties;
// constructors, getters, logic
}
public interface TextProperty {
// a marker interface
}
public enum StandardTextProperty implements TextProperty {
BOLD, ITALIC, UNDERLINED;
}
This way, anyone could add new properties if you ever deployed your code as a library for someone else to use. 这样,如果您将代码部署为库供其他人使用,则任何人都可以添加新属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.