English 中文(简体)
强制执行检查 :如果在 0 列中已经有一个字符串,则不进行检查 。
原标题:imposing a check : don t proceed if there is already a string in column number 0
  • 时间:2012-05-25 10:33:47
  •  标签:
  • java
  • string

通过以下代码,我查看所有字母组,从同一名称开始,到一个阵列,再到第二列,将所有字母组加在一起,我要保留字母组相关数字的总和。

例如:

array_1 = { {"bat","1"},
            {"rat","2"},
            {"ball","3"},
            {"run","4"},
            {"lizard","5"}
          }

into array_2 = { {"b","4"},
                 {"r","6"},
                 {"l",5}
                }

以下代码给出了半正确结果。 当它到达 ball 时, 问题再次添加了从 b 开始的下一个字母, 并将它作为单独的值存储。 问题在于行号42, 我标记了这一点 。 我应如何强制检查, 一旦它添加了字母数, 它就不会添加字母数 。

package keylogger;
import java.util.Arrays;
public class ArrayTester {

private static int finalLength=0;
private static String firstAlphabet[][];
private String data[][] = { 
                               {"Nokia" , "7"},
                               {"Blackberry" , "1"},
                               {"Nimbus","10"},
                               {"Apple","19"},
                               {"Ami","21"},
                               {"Bug","35"},
                               {"Native","200"},
                               {"zebra","100"},
                               {"Nine","9"}

                          };  

public void calculator() {
   try {  
    // traverse the whole array
    firstAlphabet = new String[data.length][data.length]; // set the length of firstAlphabet array

    for(int i=0;i<data.length;i++) {
        firstAlphabet[i][0] = data[i][0].substring( 0, 1); // get the first alphabet
        firstAlphabet[i][1] = data[i][1];
        int k = i+1;
        int v = k;
        int t=0;
        for(int j=0;j<data.length-v;j++) {
            System.out.println("Inner for loop" + j);
            String s = data[k][0];
// line 42:
            if(firstAlphabet[i][0].compareToIgnoreCase(s.substring(0, 1))==0) { 
               System.out.println("If statement");
               firstAlphabet[i][0] = s.substring(0, 1);
               Integer z = Integer.parseInt(data[k][1]) + Integer.parseInt(firstAlphabet[i][1]);
               firstAlphabet[i][1] = z.toString();                   
            }
            k++;
        }   
    }               
  }catch(Exception exc) {
     exc.printStackTrace();
   }
}

public static void main(String args[]) {
    ArrayTester o = new ArrayTester();
    o.calculator();
    for(String s[] : firstAlphabet) {
        for(String x : s) {
            System.out.println(x);
        }
    }
}
}

OUTPUT

Inner for loop0

Inner for loop1

If statement

Inner for loop2

Inner for loop3

Inner for loop4

Inner for loop5

If statement

Inner for loop6

Inner for loop7

If statement

Inner for loop0

Inner for loop1

Inner for loop2

Inner for loop3

If statement

Inner for loop4

Inner for loop5

Inner for loop6

Inner for loop0

Inner for loop1

Inner for loop2

Inner for loop3

If statement

Inner for loop4

Inner for loop5

If statement

Inner for loop0

If statement

Inner for loop1

Inner for loop2

Inner for loop3

Inner for loop4

Inner for loop0

Inner for loop1

Inner for loop2

Inner for loop3

Inner for loop0

Inner for loop1

Inner for loop2

Inner for loop0

Inner for loop1

If statement

Inner for loop0

N

226

null

null

null

null

null

null

null

B

36

null

null

null

null

null

null

null

N

219

null

null

null

null

null

null

null

A

40

null

null

null

null

null

null

null

A

21

null

null

null

null

null

null

null

B

35

null

null

null

null

null

null

null

N

209

null

null

null

null

null

null

null

z

100

null

null

null

null

null

null

null

N

9

null

null

null

null

null

null

null

如果我们注意到与字母表相关的总和是正确的。 唯一的问题是重复。 例如 N=219, 即200+19

最佳回答

重复是因为您不把选中的字母标记为脏兮兮的。

所以在第一个循环中,N已经得到了最后的计算, 但以你目前的逻辑 当尼姆布斯处于循环状态时, 你会完成整个处理过程, 因为你还没有把N标记为脏。

public void calculator() {
        List<String> marked = new ArrayList<String>();
        try {
            // traverse the whole array
            firstAlphabet = new String[data.length][2]; // set the length of first Alphabet array
            for (int i = 0; i < data.length; i++) {
                String firstLetter = data[i][0].substring(0, 1);
                if(marked.contains(firstLetter)){
                    continue;
                }
                marked.add(firstLetter);
                firstAlphabet[i][0] = firstLetter; // get the first alphabet
                firstAlphabet[i][1] = data[i][1];
                int k = i + 1;
                int v = k;
                int t = 0;
                for (int j = 0; j < data.length - v; j++) {
                    System.out.println("Inner for loop" + j);
                    String s = data[k][0];
                    if (firstAlphabet[i][0].equalsIgnoreCase(s.substring(0,
                            1))) { // line 42
                        System.out.println("If statement");
                        firstAlphabet[i][0] = s.substring(0, 1);
                        Integer z = Integer.parseInt(data[k][1])
                                + Integer.parseInt(firstAlphabet[i][1]);
                        firstAlphabet[i][1] = z.toString();
                    }
                    k++;
                }
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }
问题回答

基本上你可以做的是:

// words is an array of tuples (word, frequency); that should probably be a class in your java code
// sums is a HashMap<Character, Integer>
foreach word, frequency in words:
     letter = word[0]
     sums[letter] = sums[letter] + frequency

以 Java 写起来应该比较简单, 也比您目前的代码更快和简单。 您唯一需要做的是检查 sumps[letter] 是否已经存在, 如果不是以 频率 初始化它的话 - 太糟糕了, Java 无法提供这类事物的默认数据 。

你可以:

  1. Use a more convenient data structure, such as a Map<Character, Integer>.
  2. 如果改变数据结构不适合您, 您也可以在开始迭代前使用自定义 < code> comparator 排序输入数组。 见 < a href=" http://docs. oracle. com/ javase/6/ docs/ api/ java/ util/ Arrays. html# sort% 28T% 5b% 5d,% 20java. util. comparator% 29" rel= "nofolpol" @ code> Arrays.sort ()

    Arrays.sort(data, new Comparator<String[]>() {
        public int compare(String[] o1, String[] o2) {
            Character letter1 = o1[0].charAt(0);
            Character letter2 = o2[0].charAt(0);
            return letter1.compareTo(letter2);
        }
    });
    




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签