相关疑难解决方法(0)

如何找到两个字符串数组的联合

我试图找到两个字符串数组的联合.我创建了一个新数组,并将第一组中的所有数据复制到新数组中.我无法将第二组中的信息添加到新数组中.

我需要使用循环来搜索第二个数组并找到重复项.我一直在接受ArrayIndexOutOfBoundsException.

这是我目前的代码:

static String[] union(String[] set1, String[] set2) {
    String union[] = new String[set1.length + set2.length];

    int i = 0;
    int cnt = 0;

    for (int n = 0; n < set1.length; n++) {
        union[i] = set1[i];
        i++;
        cnt++;
    }

    for (int m = 0; m < set2.length; m++) {
        for (int p = 0; p < union.length; p++) {
            if (set2[m] != union[p]) {
                union[i] = set2[m];
                i++;
            }
        }
    }
    cnt++;

    union = downSize(union, cnt); …
Run Code Online (Sandbox Code Playgroud)

java arrays

1
推荐指数
1
解决办法
6573
查看次数

配置IntelliJ Java异常断点

有没有办法配置 IntelliJ Java 异常断点,使其仅在堆栈跟踪中的底层类是特定类时触发?例如,对于下面的堆栈跟踪,我只想在底行包含 class 时中断ComputeLCAInBinaryTreeSpec

java.lang.IndexOutOfBoundsException: Index: 4, Size: 4

    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at com.common.BinaryTreeNode.buildBinaryTree(BinaryTreeNode.groovy:62)
    at com.common.BinaryTreeNode.buildBinaryTree(BinaryTreeNode.groovy:76)
    at com.common.BinaryTreeNode.buildBinaryTrees_closure1(BinaryTreeNode.groovy:53)
    at groovy.lang.Closure.call(Closure.java:426)
    at com.common.BinaryTreeNode.buildBinaryTrees(BinaryTreeNode.groovy:51)
    at com.elementsofprogramminginterviews.binarytrees.ComputeLCAInBinaryTreeSpec.computes LCA of two nodes of a binary tree_closure1(ComputeLCAInBinaryTreeSpec.groovy:65)
    at groovy.lang.Closure.call(Closure.java:426)
    at groovy.lang.Closure.call(Closure.java:442)
    at com.elementsofprogramminginterviews.binarytrees.ComputeLCAInBinaryTreeSpec.computes LCA of two nodes of a binary tree(ComputeLCAInBinaryTreeSpec.groovy:47)
Run Code Online (Sandbox Code Playgroud)

java breakpoints exception intellij-idea conditional-statements

0
推荐指数
1
解决办法
631
查看次数

如何避免获取java.lang.ArrayIndexOutOfBoundsException

我正在尝试编写一个通过写入文本文件来模拟数据库的程序.我可以读取一个充满数据的文本文件,然后将其转换为字节数组以存储到另一个文本文件中.我遇到的问题是因为我正在从字符串转换为字节数组我不断得到java.lang.ArrayIndexOutOfBoundsException:8.我将值硬编码到我的for循环中,因此它不应该是无效的索引数组但似乎没有解决问题.这是我的函数,错误显示在:

public void writeBucket(int bucket, String importFile, String[][] allrecords)
{

  theDisk = new FakeDisk();

  for(int z = 0; z < bucket; z++)
  {

    try
     {

      for(int j = 0; j < 7; z++)//for(int j = 0; j < allrecords[z].length; z++)
      {

        if(allrecords[z][j] == null) //this is the line where the error shows up
        {
          continue;
        }

        theDisk.writeSector(z, allrecords[z][j].getBytes());
      }
     }
     catch(Exception e)
     {
         //System.out.println(e.getMessage());//this prints the number 8 when not commented out
       continue;
     }

  }

  try
  {

    FileWriter fwrite = …
Run Code Online (Sandbox Code Playgroud)

java

-1
推荐指数
1
解决办法
4672
查看次数

排序List时的ArrayIndexOutOfBoundsException

我在排序中面临ArrayIndexOutOfBoundsException.我的代码在这里:

Collections.sort(mutualFriends, new Comparator<FriendInfo>() {
   public int compare(FriendInfo s1, FriendInfo s2) {
       return s1.name.compareToIgnoreCase(s2.name);
   }
});
Run Code Online (Sandbox Code Playgroud)

日志在这里:

    STACK_TRACE=java.lang.ArrayIndexOutOfBoundsException
    at java.util.Collections.sort(Collections.java:1970)
    at com.platinumapps.fragments.Mutual_Friends_Fragment$1.onComplete(Mutual_Friends_Fragment.java:138) 
    at com.platinumapps.facedroid.AsyncRequestListener.onComplete(AsyncRequestListener.java:59) 
    at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:328)
Run Code Online (Sandbox Code Playgroud)

我的共同朋友名单如下:

private List<FriendInfo> mutualFriends = new ArrayList<FriendInfo>();
Run Code Online (Sandbox Code Playgroud)

有任何想法来解决这个问题.提前致谢.

java

-2
推荐指数
1
解决办法
2054
查看次数