[英]Same consistent-hashing algorithm implementation for Java and Python program
[英]Java Program is not consistent?
我從數據庫中以 0 或 1 格式(如 001、100、111)獲取一個 3 位數的值。根據該值,我必須執行一些操作,我已經為其編寫了代碼,但它並不一致。 它對 3 個值按預期工作,然后我將其添加到ArrayList
。 但是,當我將少於 3 個或多於 3 個值傳遞給ArrayList
時,它將不起作用
以下是我創建的程序,請幫助我糾正該程序。
package com.company;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// write your code here
String num;
ArrayList<String> al = new ArrayList<String>();
al.add("100");
al.add("111");
al.add("010");
//al.add("111");
String[] array;
if (al.size() > 0) {
for (int i = 0; i < al.size(); i++) {
array = al.get(i).split("");
System.out.println("Size of array is" + array.length);
for (int j = 0; j < al.size(); j++) {
System.out.println("size at position" + j + " is " + array[j]);
if (array[j].equals("1") && j == 0) {
System.out.println("PODS Enalbe");
} else if (array[j].equals("1") && j == 1) {
System.out.println("BAM Enalbe");
} else if (array[j].equals("1") && j == 2) {
System.out.println("PNDE Enalbe");
}
}
}
} else {
System.out.println("No value found in PTM_TRIGGER Table");
}
}
}
在您的內部循環中,您需要檢查array.length()
,而不是al.size()
。 我對您的代碼進行了一些更改以使其更具可讀性(和功能)
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
boolean isPODSEnable;
// write your code here
ArrayList<String> al = new ArrayList<String>();
al.add("100");
al.add("111");
al.add("010");
String[] array;
if (al.size() > 0) {
for (int i = 0; i < al.size(); i++) {
array = al.get(i).split("");
System.out.println("Size of array is" + array.length);
for (int j = 0; j < array.length; j++) {
System.out.println("size at position" + j + " is " + array[j]);
if ( "1".equals(array[j])) {
switch(j) {
case 1: // not 0. Your array is [,1,0,0], not [1,0,0]
System.out.println("PODS Enalbe");
break;
case 2: System.out.println("BAM Enalbe");
break;
case 3: System.out.println("PNDE Enalbe");
break;
}
}
}
}
} else {
System.out.println("No value found in PTM_TRIGGER Table");
}
}
}
正如我在評論中提到的,內循環的停止條件是錯誤的。 但是我覺得你的方法也有點過於復雜了。 您實際上不需要將String
拆分為String[]
,您可以使用charAt
直接訪問字符。 我還建議在循環List
時使用增強的 for 循環,這樣您就不會犯使用錯誤停止條件的錯誤。
這將產生如下內容:
public static void main(String[] args) {
List<String> al = new ArrayList<String>();
al.add("100");
al.add("111");
al.add("010");
al.add("1000");
if (al.size() > 0) {
for(String s : al) {
if(s.length() != 3) {
System.out.println("String has invalid length");
continue;
}
if(s.charAt(0) == '1') {
System.out.println("PODS Enable");
}
if(s.charAt(1) == '1') {
System.out.println("BAM Enable");
}
if(s.charAt(2) == '1') {
System.out.println("PNDE Enable");
}
System.out.println();
}
} else {
System.out.println("No value found in PTM_TRIGGER Table");
}
}
對於長度不正確的字符串會發生什么取決於您。 在我上面的程序中,它只是忽略了它們。
導入 java.util.ArrayList;
公共類測試類{
public static void main(String[] args) {
boolean isPODSEnable = false;
// write your code here
ArrayList<String> al = new ArrayList<String>();
al.add("100");
al.add("111");
al.add("010");
al.add("0110");
al.add("000");
if (al.size() > 0) {
for (String s : al) {
int index = s.indexOf("1");
switch (index) {
case 0: // not 0. Your array is [,1,0,0], not [1,0,0]
System.out.println("PODS Enalbe");
break;
case 1:
System.out.println("BAM Enalbe");
break;
case 2:
System.out.println("PNDE Enalbe");
break;
default:
System.out.println("Not Found");
break;
}
}
} else {
System.out.println("No value found in PTM_TRIGGER Table");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.