[英]I want to know how to store some data in Matlab which I can later call from Java code?
[英]I got a way of removing nulls from a String[], how can i make a method for that function and later call on it? Java
我是 java 的新手,剛開始使用 String arrays。 我有一個 function 用於刪除所有空值,因為我在代碼中多次使用它,所以我認為一種方法是合適的。 因此,當我有一個包含 Null 的數組時,我可以調用該特定方法並刪除所有這些方法。
現在 function 是:
int countNull = 0;
for (int i = 0; i < myArray.length; i++) {
if (myArray[i] == null) {
countNull++;
}
}
String[] noNull = new String[myArray.length - countNull];
for (int i = 0, j = 0; i < myArray.length; i++) {
if (myArray[i] != null)
noNull[j] = myArray[i];
j++;
}
我如何將它變成我可以調用的方法並簡單地說出myArray = noNull
的內容?
你可以寫一個方法:
public static String[] removeNulls(String[] array) {
int countNull = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
countNull++;
}
}
String[] noNull = new String[array.length - countNull];
for (int i = 0, j = 0; i < array.length; i++) {
if (myArray[i] != null)
noNull[j] = myArray[i];
j++;
}
return noNull;
}
我會將此方法放入名為ArrayUtils
的 class 中:
final class ArrayUtils {
private ArrayUtils() {}
// put the method here...
}
現在你可以像這樣使用它:
someArray = ArrayUtils.removeNulls(someArray);
您只需要將上面的所有代碼包裝到 static 方法中,該方法將 myArray 作為參數並返回 noNull。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.