我正在尝试实现执行以下 *** 作的算法:
如果存储在变量compHand中的数字存在,则其索引将存储在indexArray中,并且该索引将添加到banIndex()方法中,以便该索引永远不会再被考虑用于进一步 *** 作.
要么
如果列表中任何两个数字的总和等于compHand,则这些数字的索引将存储在indexArray中,并将添加到banIndex()中,以便它们永远不会被考虑用于任何进一步的 *** 作.
实际上,算法工作正常,但是如果hashMap的最后一个值是10,那么,10将会显示两次吗?它应该只显示一次.为什么?
例如:根据populateHash()的算法结果将是:5,6,7,7
它应该是:5,6,7
知道为什么会这样吗?
码
public class Test {static HashMap<Integer, Integer> h1 = new HashMap<Integer, Integer>();public static voID main(String[] args) { int compHand = 10; populateHash(Test.h1); int iter = -1; int [] indexArray = new int[(Test.h1.size())]; HashMap<Integer, Integer> bannedindexHash = new HashMap<Integer, Integer>(); for (int i=1; i<=Test.h1.size(); i++) { if (! isBannedindex(bannedindexHash, i)) { if (i == Test.h1.size()) { if (compHand == Test.h1.get(i)) { indexArray[++iter] = i; banIndex(bannedindexHash, i); }// end if }else { if (compHand == Test.h1.get(i)) { indexArray[++iter] = i; banIndex(bannedindexHash, i); }//end if else { for (int j=i+1; j<=Test.h1.size(); j++) { if ( (! isBannedindex(bannedindexHash, i)) && (! isBannedindex(bannedindexHash, j)) ) { if ( (compHand == (Test.h1.get(i)+Test.h1.get(j))) || (compHand == Test.h1.get(j)) ) { if (compHand == (Test.h1.get(i)+Test.h1.get(j))) { indexArray[++iter] = i; indexArray[++iter] = j; banIndex(bannedindexHash, i); banIndex(bannedindexHash, j); break; }//end if else { if (compHand == Test.h1.get(j)) { indexArray[++iter] = j; banIndex(bannedindexHash, j); }// end if }// end else }// end if-condition || }// end if-condition && }// end for (j) }// end else }//end else }// end ! isBannedindex(bannedindexHash, i) }// end for(i) if (iter > -1) { System.out.println("iter > -1"); for (int i=0; i<indexArray.length; i++) { System.out.println(indexArray[i]); } }}private static boolean isBannedindex(HashMap<Integer, Integer> _bannedindexHash, int index) { // Todo auto-generated method stub //Log.i(TAG, "@isBannedindex(): "); if (!_bannedindexHash.isEmpty()) { for (int i=1; i<_bannedindexHash.size(); i++) if (index == _bannedindexHash.get(i)) return true; return false; }else return false;}private static voID banIndex(HashMap<Integer, Integer> _bannedindexHash, int index) { // Todo auto-generated method stub //Log.i(TAG, "@banIndex(): "); if (_bannedindexHash != null) _bannedindexHash.put(_bannedindexHash.size()+1, index);}private static voID populateHash(HashMap<Integer, Integer> hash) { // Todo auto-generated method stub hash.put(1, 1); hash.put(2, 3); hash.put(3, 1); hash.put(4, 1); hash.put(5, 10); hash.put(6, 10); hash.put(7, 10); /*hash.put(8, 7); hash.put(9, 1); hash.put(10, 10); hash.put(11, 5); hash.put(12, 8); hash.put(13, 1); hash.put(14, 1); hash.put(15, 6); hash.put(16, 1); hash.put(17, 1); hash.put(18, 2);*/ }}
解决方法:
我重写了你的代码并丢弃了多余的if-else级联.
我的解决方案生成以下输出(由于预定义的数组长度,存在0个数字):
iter> -1
五
6
7
0
0
0
0
public class Test { public static voID main(final String[] args) { HashMap<Integer, Integer> testHashes = new HashMap<Integer, Integer>(); int compHand = 10; populateHash(testHashes); int iter = -1; int[] indexArray = new int[(testHashes.size())]; HashMap<Integer, Integer> bannedValues = new HashMap<Integer, Integer>(); for (int i = 1; i <= testHashes.size(); i++) { if (!isBannedindex(bannedValues, i)) { if (compHand == testHashes.get(i)) { indexArray[++iter] = i; banIndex(bannedValues, i); } else { for (int j = i + 1; j <= testHashes.size(); j++) { if (!isBannedindex(bannedValues, j)) { if(compHand == testHashes.get(i) + testHashes.get(j)) { indexArray[++iter] = i; indexArray[++iter] = j; banIndex(bannedValues, i); banIndex(bannedValues, j); break; } else { if (compHand == testHashes.get(j)) { indexArray[++iter] = j; banIndex(bannedValues, j); } } } } } } } if (iter > -1) { System.out.println("iter > -1"); for (int i = 0; i < indexArray.length; i++) { System.out.println(indexArray[i]); } } } private static boolean isBannedindex( final HashMap<Integer, Integer> banned, final int index) { return !banned.isEmpty() && banned.values().contains(index); } private static voID banIndex(final HashMap<Integer, Integer> banned, final int index) { if (banned != null) banned.put(banned.size() + 1, index); } private static voID populateHash(final HashMap<Integer, Integer> hash) { // Todo auto-generated method stub hash.put(1, 1); hash.put(2, 3); hash.put(3, 1); hash.put(4, 1); hash.put(5, 10); hash.put(6, 10); hash.put(7, 10); /* * hash.put(8, 7); hash.put(9, 1); hash.put(10, 10); hash.put(11, 5); * hash.put(12, 8); hash.put(13, 1); hash.put(14, 1); hash.put(15, 6); * hash.put(16, 1); hash.put(17, 1); hash.put(18, 2); */ }}
编辑:另一个,更严格的版本可能看起来像这样:
import java.util.ArrayList;import java.util.List;public class Test { static List<Integer> resultIndexes = new ArrayList<Integer>(); public static voID main(final String[] args) { List<Integer> testHashes = new ArrayList<Integer>(); populateHash(testHashes); Integer compHand = new Integer(10); for(int i = 0; i < testHashes.size(); i++){ if(isBanned(i)){ continue; } Integer valueA = testHashes.get(i); if(valueA.equals(compHand)){ resultIndexes.add(i); } else { Integer valueB = compHand - valueA; int index = testHashes.indexOf(valueB); if(!isBanned(index) && index > -1 && index != i){ resultIndexes.add(i); resultIndexes.add(testHashes.indexOf(valueB)); } } } for(int i : resultIndexes){ System.out.println("Index: "+i+"; Value: "+testHashes.get(i)); } } private static boolean isBanned(final Integer i){ return resultIndexes.contains(i); } private static voID populateHash(final List<Integer> hashes) { hashes.add(1); hashes.add(3); hashes.add(1); hashes.add(1); hashes.add(10); hashes.add(10); hashes.add(10); hashes.add(7); hashes.add(1); hashes.add(10); hashes.add(5); hashes.add(8); hashes.add(1); hashes.add(1); hashes.add(6); hashes.add(1); hashes.add(1); hashes.add(2); }}
控制台日志:
指数:1;价值:3
指数:7;价值:7
指数:4;价值:10
指数:5;价值:10
指数:6;价值:10
指数:9;价值:10
指数:11;价值:8
指数:17;价值:2
以上是内存溢出为你收集整理的java – 从HashMap中排除索引全部内容,希望文章能够帮你解决java – 从HashMap中排除索引所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)