Java 基于字符串标识符检查对象字段
我有一个具有不同类型字段的对象,主要是字符串、浮点、整数 我需要编写一个方法来检查这类对象的数组,由特定字符串标识符标识的特定字段是否不为空 例如,我有以下字段:Java 基于字符串标识符检查对象字段,java,oop,object,Java,Oop,Object,我有一个具有不同类型字段的对象,主要是字符串、浮点、整数 我需要编写一个方法来检查这类对象的数组,由特定字符串标识符标识的特定字段是否不为空 例如,我有以下字段: String field1; Integer field2; String field3; 以及一个标识符: String IDENTIFIER_FIELD_1 = "IDENTIFIER_1"; String IDENTIFIER_FIELD_2 = "IDENTIFIER_2"; 如果我用标识符字段字段字段字段字段1传递数组列
String field1;
Integer field2;
String field3;
以及一个标识符:
String IDENTIFIER_FIELD_1 = "IDENTIFIER_1";
String IDENTIFIER_FIELD_2 = "IDENTIFIER_2";
如果我用标识符字段字段字段字段字段1
传递数组列表
,我只想检查字段1
,如果我用标识符字段字段字段1
和标识符字段字段字段字段2
传递数组列表
,我想同时检查字段2
Java中有没有有效的方法来检查对象的数组?我提出的唯一解决方案是使用字符串比较来检查每个字段,但这根本不起作用。您可以使用contains方法,如:
if (mylist.contains("field1") && myList.contains("field2")) {
System.out.println("Found both identifier");
}
我会在对象中放置一个方法,类似于:
public boolean isPopulated(String key){ // or int key if you're more interested in efficiency than clarity
switch(key){
case "field1":
if (field1 != null && !field1.equals("")) return true;
return false;
break;
case "field2":
etc
这样,您可以为每个字段定义“空”的含义
然后说明显而易见的情况,您的检查方法如下(假设您使用的是整数字段而不是字符串)
公共布尔区域填充(ArrayList对象、ArrayList字段){
for(整型字段:字段){
用于(MyObject对象:对象){
if(object.isPopulated(field)==false){
返回false;
}
}
}
返回true;
}
如果您担心字符串比较的成本,您可以在某处创建字段名到字段整数的哈希映射,并转换
在检查方法开始时,将字符串转换为整数数组列表,这将节省大部分字符串比较的代价。您可以使用一些反射,这可能是一种“过度杀戮”方法。这个方法有一个缺陷,你需要知道变量的确切名称,但我相信你已经知道了
public class TestObject {
//Some fields.
private int firstInt;
private int secondInt;
private String firstString;
public TestObject(int firstInt, int secondInt, String firstString) {
this.firstInt = firstInt;
this.secondInt = secondInt;
this.firstString = firstString;
}
public void checkForNulls(String... fieldsToSearch) throws IllegalArgumentException, IllegalAccessException {
boolean isValid = true;
//Here we retrieve all the fields we need from our class.
for (String fieldName : fieldsToSearch) {
//Here is the method to get a field by name.
try {
Field field = this.getClass().getDeclaredField(fieldName);
//Change access modifier so we can get the value.
field.setAccessible(true);
//Get the field value of this instance.
if (field.get(this) == null) {
isValid = false;
System.out.println("Fix him! Field " + field.getName() + " equals null!");
}
} catch (Exception e) {
//There can be no field with such name, you need to do something here.
}
}
if (isValid) {
System.out.println("This objects fields are fine.");
}
}
}
现在主要的方法是:
public class Test {
//Names of the fields, you actually need to know them.
public static final String FIRST_INT_INDENTIFIER = "firstInt";
public static final String SECOND_INT_INDENTIFIER = "secondInt";
public static final String FIRST_STRING_INDENTIFIER = "firstString";
public static void main(String[] args) {
TestObject[] testArray = new TestObject[10];
//Fill the array with some objects.
for (int i = 0; i < testArray.length; i++) {
testArray[i] = new TestObject(i, i * 2, "Number: " + i);
if (i % 3 == 0) {
testArray[i] = new TestObject(i, i * 2, null);
}
}
//Check the array of our objects.
for (int i = 0; i < testArray.length; i++) {
try {
testArray[i].checkForNulls(FIRST_INT_INDENTIFIER, FIRST_STRING_INDENTIFIER);
} catch (Exception e) {
//Some exception handling
}
}
}
}
公共类测试{
//字段的名称,您实际上需要知道它们。
公共静态最终字符串FIRST\u INT\u identifier=“firstInt”;
公共静态最终字符串SECOND_INT_identifier=“secondInt”;
public static final String FIRST\u String\u identifier=“firstString”;
公共静态void main(字符串[]args){
TestObject[]testArray=新的TestObject[10];
//用一些对象填充数组。
对于(int i=0;i
这不是我想要的。那么你能在问题本身中解释一下吗?这看起来是正确的解决方案,但我不确定我是否能够在对象数组中有效地使用它(而不必每次都创建列表
)。嗯,你可以检查字段的值并在第一个for循环中执行任何操作,不将其添加到列表中(按名称检索字段时)。将字段添加到列表中,以便在每个循环中立即使用它们,这确实是我的误解;)我编辑了代码。
public class Test {
//Names of the fields, you actually need to know them.
public static final String FIRST_INT_INDENTIFIER = "firstInt";
public static final String SECOND_INT_INDENTIFIER = "secondInt";
public static final String FIRST_STRING_INDENTIFIER = "firstString";
public static void main(String[] args) {
TestObject[] testArray = new TestObject[10];
//Fill the array with some objects.
for (int i = 0; i < testArray.length; i++) {
testArray[i] = new TestObject(i, i * 2, "Number: " + i);
if (i % 3 == 0) {
testArray[i] = new TestObject(i, i * 2, null);
}
}
//Check the array of our objects.
for (int i = 0; i < testArray.length; i++) {
try {
testArray[i].checkForNulls(FIRST_INT_INDENTIFIER, FIRST_STRING_INDENTIFIER);
} catch (Exception e) {
//Some exception handling
}
}
}
}