java - 在字符串不为空或为空时执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3296165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java - do while string is NOT null or empty
提问by test
OK so here is some logic thinking... What I am trying to do is loop through strings until I hit a null/empty string.. then STOP. All while putting those strings inside a string array...
好的,所以这里有一些逻辑思考......我想要做的是循环遍历字符串,直到我遇到一个空/空字符串......然后停止。同时将这些字符串放入字符串数组中......
Here is a sample of the code. I know it's wrong but hopefully to give you an idea of what I am trying to achieve:
这是代码的示例。我知道这是错误的,但希望让您了解我正在努力实现的目标:
int i;
wepN = new String[100];
int wepQty = 0;
boolean anyLeft = true;
while (anyLeft == true) {
for(i = 0;i < 100;i++) {
if (data.getItems().get(i).getName() == null) {
anyLeft = false;
System.out.println(data.getItems().get(i).getName() + " -NO MOARE");
}
wepN[i] = data.getItems().get(i).getName();
wepQty++;
}
}
回答by jayshao
You can use break to exit a for loop, same as you would a switch statement:
您可以使用 break 退出 for 循环,就像使用 switch 语句一样:
String[] wepN = new String[100];
int wepQty = 0;
for (int i=0; i < wepN.length; i++) {
if (data.getItems().get(i).getName() == null || "".equals(data.getItems().get(i).getName())) {
System.out.println(data.getItems().get(i).getName() + " -NO MOARE");
break;
}
wepN[i] = data.getItems().get(i).getName();
wepQty++;
}
回答by Blessed Geek
There are a few considerations depending on whether the original was a collection.
有几个考虑因素取决于原件是否是一个集合。
If you had an array Data[] data,
如果你有一个数组 Data[] 数据,
String[] dataCopy = new String[data.length];
int i = 0;
for (Data datum: data){
if (datum==null)break;
dataCopy[i++] = datum;
}
But that is not optimal because you would be assigning more array cells than necessary if the original data had 100 cells but the 50th cell is where the empty string is found.
但这不是最佳选择,因为如果原始数据有 100 个单元格,但第 50 个单元格是找到空字符串的位置,您将分配比需要更多的数组单元格。
Using an ArrayList would let the JVM manage the expansion of cells, so that at the end of it you just convert the ArrayList to an array using toArray(). It's not a conversion really, but toArray withdraws the internally managed array from the ArrayList.
使用 ArrayList 将使 JVM 管理单元格的扩展,因此在它结束时您只需使用 toArray() 将 ArrayList 转换为数组。这不是真正的转换,而是 toArray 从 ArrayList 中取出内部管理的数组。
ArrayList<String> dataList = new ArrayList<String>(data.length);
for (Data datum: data){
if (datum==null)break;
dataList.add(datum);
}
String[] dataCopy = {};
dataCopy = datalist.toArray(dataCopy);
Or if the array you are processing is a member of data:
或者,如果您正在处理的数组是数据的成员:
ArrayList<String> dataList = new ArrayList<String>(data.length);
for (Data datum: data.getItems()){
String name = datum.getName();
if (name==null)break;
dataList.add(name);
}
String[] dataCopy = {};
dataCopy = datalist.toArray(dataCopy);
Or if the original data structure implements Iterable. Let's say the class of items is Item.
或者如果原始数据结构实现了 Iterable。假设项目的类别是 Item。
Iterator<Item> itemit = data.getItems().iterator();
ArrayList<String> dataList = new ArrayList<String>(data.length);
while(itemit.hasNext()){
String name = itemit.next().getName;
if (name==null)break;
dataList.add(name);
}
String[] dataCopy = {};
dataCopy = datalist.toArray(dataCopy);
回答by S73417H
Something like this is what you are after:
像这样的事情就是你所追求的:
Collection<String> original = new LinkedList<String>();
original.add("String1");
original.add("String2");
original.add("");
original.add(null);
original.add("String 3");
Collection<String> tested = new LinkedList<String>();
for(String string: original) {
if(null != string && !string.isEmpty()) {
tested.add(string);
}
}
String[] stringArray = tested.toArray(new String[tested.size()]);
I would argue not to use array at all and just stick to the Collection type however.
我认为根本不要使用数组,而是坚持使用 Collection 类型。
If you want to stop on the first occurance of a null or empty string just do:
如果您想在第一次出现空字符串或空字符串时停止,请执行以下操作:
if(null != string && !string.isEmpty()) {
tested.add(string);
} else {
break;
}
回答by user268396
Numerous ways:
多种方式:
String currentName;
for(i=0;i<100;++i) {
currentName=data.getItems().get(i).getName();
if(currentName == null || currentName.length() ==0) {
break;
}
// continue with old code here
}
If you don't like explicit breaks:
如果您不喜欢明确的休息时间:
String currentName;
while(anyLeft) {
currentName=data.getItems().get(i).getName();
anyLeft= currentName != null && currentName.length() > 0;
if(anyLeft) {
// continue with old code here
}
}
回答by mhshams
why you need to use while here?
为什么你需要在这里使用 while ?
how about:
怎么样:
for (int i = 0; i < 100 && data.getItems().get(i).getName() != null; i++ {
wepN[i] = data.getItems().get(i).getName();
wepQty++;
}
or
或者
int i = 0;
while (data.getItems().get(i).getName() != null && i < 100) {
wepN[i] = data.getItems().get(i).getName();
wepQty++;
i++
}

