返回java中循环中存在的值
我想从循环中返回一个值。 我的代码如下:返回java中循环中存在的值,java,loops,return-value,Java,Loops,Return Value,我想从循环中返回一个值。 我的代码如下: public long findLocationId(String location) throws SystemException { long locId = 1; List<Location> locationList = LocationLocalServiceUtil.getLocations(-1,-1); for(Location findLoc : loca
public long findLocationId(String location) throws SystemException
{
long locId = 1;
List<Location> locationList = LocationLocalServiceUtil.getLocations(-1,-1);
for(Location findLoc : locationList) {
if(location == findLoc.getLocationName()) {
locId = findLoc.getLocationId();
**return locId;**
}
}
}
public long findLocationId(字符串位置)引发系统异常
{
长locId=1;
List locationList=LocationLocalServiceUtil.getLocations(-1,-1);
对于(位置findLoc:locationList){
if(location==findLoc.getLocationName()){
locId=findLoc.getLocationId();
**返回locId**
}
}
}
当我尝试将返回值放入循环时,我得到一个错误,提示包含return语句
我应该如何更改代码,以便能够从循环本身返回值
我想返回我在循环中得到的新locId值,而不是我最初设置为locId=1的值;
我想返回我从循环中得到的locId的新值必须有一个默认的return语句。因为只有返回语句也是有条件的,所以编译器将强制您使用默认的返回语句
public long findLocationId(字符串位置)引发系统异常{
长locId=1;
List locationList=LocationLocalServiceUtil.getLocations(-1,-1);
对于(位置findLoc:locationList){
if(location==findLoc.getLocationName()){
locId=findLoc.getLocationId();
返回locId;
}
}
return locId;//默认返回值
}
公共长findLocationId(字符串位置)引发系统异常
{
长locId=1;
List locationList=LocationLocalServiceUtil.getLocations(-1,-1);
对于(位置findLoc:locationList){
if(location==findLoc.getLocationName()){
locId=findLoc.getLocationId();
打破
}
}
返回locId;
}
这是因为函数中的所有控制流都应以返回长值结束
在您的例子中,假设列表中没有匹配项,因此永远不会执行return语句,这就是报告错误的原因
要解决这个问题,您可以在函数末尾添加一个带有默认值的return语句,或者如果您的逻辑允许,您可以抛出一个execption,说明找不到location
解决方案1:如果没有匹配项,则要返回1
public long findLocationId(String location) throws SystemException {
long locId = 1;
List<Location> locationList = LocationLocalServiceUtil.getLocations(-1,
-1);
for (Location findLoc : locationList) {
if (location == findLoc.getLocationName()) {
locId = findLoc.getLocationId();
break;
}
}
return locId;
}
public long findLocationId(字符串位置)引发系统异常{
长locId=1;
List locationList=LocationLocalServiceUtil.getLocations(-1,
-1);
对于(位置findLoc:locationList){
if(location==findLoc.getLocationName()){
locId=findLoc.getLocationId();
打破
}
}
返回locId;
}
解决方案2:如果找不到位置,则引发异常
public long findLocationId(String location) throws SystemException {
List<Location> locationList = LocationLocalServiceUtil.getLocations(-1,
-1);
for (Location findLoc : locationList) {
if (location == findLoc.getLocationName()) {
return findLoc.getLocationId();
}
}
throw new SystemException("Unable to find the location");
}
public long findLocationId(字符串位置)引发系统异常{
List locationList=LocationLocalServiceUtil.getLocations(-1,
-1);
对于(位置findLoc:locationList){
if(location==findLoc.getLocationName()){
返回findLoc.getLocationId();
}
}
抛出新的SystemException(“找不到位置”);
}
这是因为在if
语句中返回语句,这是有条件的。因此,如果存在控件从未进入if
块的情况,则该方法将没有返回值
因此,在方法末尾有一个默认的返回值。如果语句没有找到任何与您的位置相等的位置,则意味着将发生什么。这就是为什么会发生这种编译错误。所以您必须在方法的末尾添加一个return语句
public long findLocationId(String location) throws SystemException
{
long locId = 1;
List<Location> locationList = LocationLocalServiceUtil.getLocations(-1,-1);
for(Location findLoc : locationList) {
if(location == findLoc.getLocationName()) {
locId = findLoc.getLocationId();
**return locId;**
}
}
return
}
public long findLocationId(字符串位置)引发系统异常
{
长locId=1;
List locationList=LocationLocalServiceUtil.getLocations(-1,-1);
对于(位置findLoc:locationList){
if(location==findLoc.getLocationName()){
locId=findLoc.getLocationId();
**返回locId**
}
}
返回
}
如果不使用break语句执行循环,则应使用break语句从循环中出来
不能在for循环中使用return语句,因为此return语句将调用多次。
Iterator<Location> iterator = locationList.iterator();
while(iterator.hasNext() && Long.MIN_VALUE != locId) {
Location location = iterator.next();
if(locationName.equalsIgnoreCase(location.getLocationName())) {
locId = location.getLocationId(); // This will change the locId, so second condition will be no longer true and loop will end.
}
}
for(Iterator<Location> iterator2 = locationList.iterator();iterator.hasNext() && Long.MIN_VALUE != locId;) {
Location location = iterator.next();
if(locationName.equalsIgnoreCase(location.getLocationName())) {
locId = location.getLocationId(); // This will change the locId, so second condition will be no longer true and loop will end.
}
}
for(Location location : locationList) {
if(locationName.equalsIgnoreCase(location.getLocationName())) {
locId = location.getLocationId();
break;
}
}
并且该方法一次只需要一个return语句。
这里的break
语句只会从最里面的循环中中断出来(这里是for循环)
public long findLocationId(字符串位置)引发系统异常
{
长locId=1;
List locationList=LocationLocalServiceUtil.getLocations(-1,-1);
对于(位置findLoc:locationList){
if(location==findLoc.getLocationName()){
locId=findLoc.getLocationId();
打破
}
}
返回locId;
}
有关更多详细信息:
您的返回语句位于“if”语句中,这意味着如果“if”的计算结果为false,将无法访问该语句。您需要在“if”语句之外使用return语句。这是因为当location==findLoc.getLocationName()
时并不总是会出现这种情况。即使是这样,java编译器也不知道您给程序的是什么类型的输入,因此它会告诉您,可能存在函数不返回任何内容的情况,即使它必须返回一个long
只是返回-1L或者你的PoGRAM在函数末尾被认为是“找不到”的东西。
问题是:如果循环中的条件不满足,你的方法会返回什么?
您需要返回一个默认值,return
语句需要放在for循环之后。代码中有两个问题
必须正好有一个
public long findLocationId(String location) throws SystemException
{
long locId = 1;
List<Location> locationList = LocationLocalServiceUtil.getLocations(-1,-1);
for(Location findLoc : locationList) {
if(location == findLoc.getLocationName()) {
locId = findLoc.getLocationId();
}
}
return locId;
}
public class Main {
public static long findStringIDX(String[] myList, String myString) {
long locId = 0;
for (String s : myList) {
if (myString.equalsIgnoreCase(s)) {
return locId;
}
locId++;
}
return -1; // Not found
}
public static void main(String[] args) {
String[] myList = new String[] { "hello", "world", "bobby" };
System.out.println(findStringIDX(myList, "bobby"));
}
}
public long findLocationId(String location) throws SystemException
{
long locId = 1; // but i suggest to put -1
List<Location> locationList = LocationLocalServiceUtil.getLocations(-1,-1);
for(Location findLoc : locationList) {
if(location == findLoc.getLocationName()) {
return findLoc.getLocationId();
}
}
return locId;
}
public long findLocationId(String locationName) throws SystemException
{
if(locationName == null) { //Here we cover first issue.
throw new IllegalArgumentException("THe locationName must not be null");
}
long locId = Long.MIN_VALUE; //We declare a default value that will be returned if none match found.
Collection<Location> locationList = getLocationList(); //The location can be read from another method so we are not binded to field.
if(locationList == null || locationList.isEmpty()) {
return locId; // Or throw an exception about invalid state.
}
//Place for the logic
return locId;
}
Iterator<Location> iterator = locationList.iterator();
while(iterator.hasNext() && Long.MIN_VALUE != locId) {
Location location = iterator.next();
if(locationName.equalsIgnoreCase(location.getLocationName())) {
locId = location.getLocationId(); // This will change the locId, so second condition will be no longer true and loop will end.
}
}
for(Iterator<Location> iterator2 = locationList.iterator();iterator.hasNext() && Long.MIN_VALUE != locId;) {
Location location = iterator.next();
if(locationName.equalsIgnoreCase(location.getLocationName())) {
locId = location.getLocationId(); // This will change the locId, so second condition will be no longer true and loop will end.
}
}
for(Location location : locationList) {
if(locationName.equalsIgnoreCase(location.getLocationName())) {
locId = location.getLocationId();
break;
}
}
public long findLocationId(String locationName) throws SystemException
{
if(locationName == null) { //Here we cover fist issue.
throw new IllegalArgumentException("THe locationName must not be null");
}
Collection<Location> locationList = getLocationList(); //The location can be read from another method so we are not binded to field.
if(locationList == null) {
throw new IllegalStateException("THe location list was not initialized");
}
for(Location location : locationList) {
if(locationName.equalsIgnoreCase(location.getLocationName())) {
return location.getLocationId(); //We exit from the method.
}
}
throw new SystemException("Could not found location for name:" + locationName);
}