【问题标题】:Union of Two Int Arrays in Java [closed]Java中两个Int数组的联合[关闭]
【发布时间】:2013-02-18 17:48:07
【问题描述】:

这是我的代码

    import java.util.Arrays;
class union {
    public static void main(String args[]) {

int[] A = new int[1000];
int[] B = new int[1000];
int[] C = new int[2000];    
    for (int i= 3;i < 1000 ; i += 3) {
        A[i/3] = i; 
    }
    for (int j=5; j < 1000; j += 5) {
        B[j/5] = j;
    }

    C = unionArrays(A, B);

    }
}

编译时出现以下错误

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method unionArrays(int[], int[]) is undefined for the type union

    at union.main(union.java:15)

【问题讨论】:

  • unionArrays 在哪里。
  • unionArrays(int[], int[]) 定义在哪里?
  • 我猜你正在使用你用其他语言学习的函数/方法。阅读 java 文档。查看该类公开并提供了哪些方法。
  • 这里没有问题。关闭过于本地化

标签: java arrays exception union


【解决方案1】:

在这种情况下,您可能希望使用 HashSet 而不是这样的数组:

import java.util.HashSet;
public class ProjectEulerOne{
 public static void main(String[] args){
 HashSet<Integer> SetA= new HashSet<Integer>();
  for(int i = 3; i<1000; i+=3){
     SetA.add(new Integer(i));
  }
  for(int i = 3; i<1000; i+=3){
     SetA.add(new Integer(i));
  }
  int total =0;
  Iterator<Integer> iter = numSet.iterator();
  Integer current;
  while(iter.hasnext()){
   current = iter.next();
   total += current.intValue();
  }
 }
}

这里是HashSet javadoc,我完成了这个是因为“迭代器很奇怪”,但祝你的 Project Euler 的其余部分好运。

如果你想用我推荐的数学方法来做,我推荐使用支持列表数学的 APL / J / K。

【讨论】:

  • 它要求创建类 因为它没有定义。
  • 它是整数而不是整数,抱歉编辑了主帖
【解决方案2】:

在您的班级中定义unionArrays(int[], int[])。检查类似的SO post here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2021-04-10
    • 2011-11-26
    • 1970-01-01
    • 2016-01-04
    相关资源
    最近更新 更多