在Java中使用字符串返回对象
我对此有点困惑,因为我对Java还是相当陌生的 这是我的问题: 我需要通过使用字符串将对象传递给另一个对象来返回该对象。也就是说,我想将一个字符串传递给函数(在Java中使用字符串返回对象,java,Java,我对此有点困惑,因为我对Java还是相当陌生的 这是我的问题: 我需要通过使用字符串将对象传递给另一个对象来返回该对象。也就是说,我想将一个字符串传递给函数(getObject,在本例中),然后使用其getCode函数将其与单元对象的ArrayList进行比较 到目前为止,我所拥有的: private Unit getUnitObject(String unit1Code) { for (int i = 0; i < units.size(); i++) { Unit curr
getObject
,在本例中),然后使用其getCode
函数将其与单元对象的ArrayList
进行比较
到目前为止,我所拥有的:
private Unit getUnitObject(String unit1Code) {
for (int i = 0; i < units.size(); i++) {
Unit currUnit = units.get(i);
String unitCode = currUnit.getUnitCode();
if (unit1Code == unitCode) {
Unit selectedUnit = currUnit;
return selectedUnit;
}
}
}
私有单元getUnitObject(字符串unit1Code){
对于(int i=0;i
它给了我一个错误——“此方法必须返回unit类型的结果”
我试着将返回移出for循环,但仍然没有成功?
我可以这样做吗?问题是,只有当if语句为真时才返回某些内容,但如果if语句为假,则还需要返回某些内容。一个类中只有一个return语句是很好的实践。 在开始时创建一个返回类型的变量(用null或标准值实例化),并根据您的条件更改此变量。 最后返回该变量
private Unit getUnitObject(String unit1Code) {
Unit selectedUnit = null;
for (int i = 0; i < units.size(); i++) {
Unit currUnit = units.get(i);
String unitCode = currUnit.getUnitCode();
if (unit1Code == unitCode) {
selectedUnit = currUnit;
}
}
return selectedUnit;
}
私有单元getUnitObject(字符串unit1Code){
单位selectedUnit=null;
对于(int i=0;i
问题在于,只有当if语句为真时才返回某些内容,但当if语句为假时还需要返回某些内容。一个类中只有一个return语句是很好的实践。
在开始时创建一个返回类型的变量(用null或标准值实例化),并根据您的条件更改此变量。
最后返回该变量
private Unit getUnitObject(String unit1Code) {
Unit selectedUnit = null;
for (int i = 0; i < units.size(); i++) {
Unit currUnit = units.get(i);
String unitCode = currUnit.getUnitCode();
if (unit1Code == unitCode) {
selectedUnit = currUnit;
}
}
return selectedUnit;
}
私有单元getUnitObject(字符串unit1Code){
单位selectedUnit=null;
对于(int i=0;i
问题在于,如果没有找到匹配项,则不会返回任何内容。试试这个:
private Unit getUnitObject(String unit1Code) {
for (int i = 0; i < units.size(); i++) {
Unit currUnit = units.get(i);
String unitCode = currUnit.getUnitCode();
if (unit1Code.equals(unitCode)) {
return currUnit;
}
}
return null;
}
私有单元getUnitObject(字符串unit1Code){
对于(int i=0;i
请注意,我也在使用
.equals()
比较字符串对象。如果没有匹配项,您可能希望返回比null
更好的内容。问题是,如果找不到匹配项,则不返回任何内容。试试这个:
private Unit getUnitObject(String unit1Code) {
for (int i = 0; i < units.size(); i++) {
Unit currUnit = units.get(i);
String unitCode = currUnit.getUnitCode();
if (unit1Code.equals(unitCode)) {
return currUnit;
}
}
return null;
}
private Unit getUnitObject(String unit1Code) {
Unit selectedUnit = new Unit();
for (int i = 0; i < units.size(); i++) {
Unit currUnit = units.get(i);
String unitCode = currUnit.getUnitCode();
if (unit1Code.compareTo(unitCode)) {
selectedUnit = currUnit;
}
}
return selectedUnit;
}
私有单元getUnitObject(字符串unit1Code){
对于(int i=0;i
请注意,我也在使用.equals()
比较字符串对象。如果没有匹配项,您可能希望返回比null更好的值。私有单元getUnitObject(字符串unit1Code){
private Unit getUnitObject(String unit1Code) {
Unit selectedUnit = new Unit();
for (int i = 0; i < units.size(); i++) {
Unit currUnit = units.get(i);
String unitCode = currUnit.getUnitCode();
if (unit1Code.compareTo(unitCode)) {
selectedUnit = currUnit;
}
}
return selectedUnit;
}
单位选择单位单位=新单位();
对于(int i=0;i
您必须始终返回一个Unit对象,不仅uni1Code等于UnitCode。
因此,在方法的开头创建一个变量,如果if为true,则进行赋值,最后返回。它将返回空单元或currentUnit..专用单元getUnitObject(字符串unit1Code){
单位选择单位单位=新单位();
对于(int i=0;i
您必须始终返回一个Unit对象,不仅uni1Code等于UnitCode。
因此,在方法的开头创建一个变量,如果if为true,则进行赋值,最后返回。它将返回空单元或当前单元。使用此配置,我希望它能正常工作
private Unit getUnitObject(String unit1Code) {
Unit selectedUnit=null;
for (int i = 0; i < units.size(); i++) {
Unit currUnit = units.get(i);
String unitCode = currUnit.getUnitCode();
if (unit1Code.equals(unitCode)) {
selectedUnit = currUnit;
}
}
return selectedUnit;
}
私有单元getUnitObject(字符串unit1Code){
单位selectedUnit=null;
对于(int i=0;i
使用此配置,我希望它能正常工作
private Unit getUnitObject(String unit1Code) {
Unit selectedUnit=null;
for (int i = 0; i < units.size(); i++) {
Unit currUnit = units.get(i);
String unitCode = currUnit.getUnitCode();
if (unit1Code.equals(unitCode)) {
selectedUnit = currUnit;
}
}
return selectedUnit;
}
私有单元getUnitObject(字符串unit1Code){
单位selectedUnit=null;
对于(int i=0;i
请不要将字符串与=
进行比较。。我想这可能是第一天没有这个==
vsequals
问题:。(现在对于你的问题,如果if
不满足,你必须返回一些东西。我从来都不知道equals()函数。非常感谢!请不要比较字符串