【问题标题】:Checking if array is subset of other array. Java检查数组是否是其他数组的子集。爪哇
【发布时间】:2010-11-03 18:46:46
【问题描述】:

我必须创建一个包含 100 个数字的数组,然后随机打乱前 20 个数字以获得 2 个不同的数组; A 和 B。

对于这个作业,我必须检查数组 A 的前 20 个数字是否是数组 B 的前 20 个数字的子集

到目前为止,我有这个:

import java.util.Random;

public class opgave6 {
 public static void main(String[] args){

  Verzameling a = new Verzameling(20, 3);
  Verzameling b = new Verzameling(20, 4);

  System.out.println(Verzameling.deelverzamelingVan());
 }
}



class Verzameling {
 int[] elementen;
 int elementen2;
 static int aantal2;

 Verzameling(int aantal , int seed) {
  elementen = new int[100];
  int aantal2 = aantal;

  for(int i = 0; i < 100; i++){
   elementen[i] = i;
  }

  Random random1 = new Random(seed);

  for(int i = 0; i < 100; i++){
   int r = random1.nextInt(100);
   int temp;
   temp = elementen[i];
   elementen[i] = elementen[r];
   elementen[r] = temp;
  } 

  printVerzameling(aantal);


 }

 Verzameling(int seed) {

 }

 void printVerzameling(int aantal){
  for (int i = 0; i < aantal; i++){
   System.out.print(elementen[i] + " ");
  } 
  System.out.println();
 }

 static boolean deelverzamelingVan() {
  while (true) {
   for(i = 0; i < aantal2; i++){
    for(j = 0; j < aantal2; j++){
     if(Verzameling.a.elementen[i] = Verzameling.b.elementen[j])
     break;
    }
   }

  } 
 }


}

但是,它根本不起作用,因为我不知道如何比较对象 A 的 elementen[i] 和对象 B 的 element[j]。如何使用静态方法比较两个对象的不同元素同班。

(所以 Verzameling A 和 B 都是 Verzameling 类的实例,使用静态方法检查 A 是否是 B 的子集。如何从 Verzameling A 和 B 获取数组中的数字?)

如果有不清楚的地方请告诉我!我不需要完整的解决方案,只需要如何从对象 A 和 B 访问 elementen[i] 的值。谢谢!

编辑:

这是问题所在:

if(Verzameling.a.elementen[i] == Verzameling.b.elementen[j])

感谢您的评论,但是当我编译时它仍然出错。它说它找不到关于 verzameling.a.elementen、i、verzameling.b.elementen 和 j 的符号。我想我把它命名错了,可以通过说:classname.objectname.variable of object 来调用变量吗?

【问题讨论】:

  • 感谢@hvgotcodes 的布局编辑,我似乎从来没有让它工作?你是怎么做到的?
  • 您是否复制并粘贴了此代码?还是输入SO?你不应该有一个=;它应该是==。但是,它不会编译,所以这可能对你没有帮助。
  • @javaa,我在编辑器中使用了代码格式化图标——它是一堆 1 和 0。
  • 我想知道你为什么不能使用 List.containsAll(...) 和 Arrasy.asList(....)
  • @Pangea: Arrays.asList 在给定原始数组时不会像您期望的那样工作。 List&lt;int[]&gt; list = Arrays.asList(someIntArray)

标签: java subset


【解决方案1】:

你的 if 语句被抬高了:

if(Verzameling.a.elementen[i] = Verzameling.b.elementen[j])

你正在做作业,你没有测试任何东西。

试试

Verzameling.a.elementen[i].equals(Verzameling.b.elementen[j])

Verzameling.a.elementen[i] == Verzameling.b.elementen[j]

请注意,如果您使用 equals 方法,则需要确保 Verzameling.a.elementen[i] 不为 null。另请注意,对于数字 == 是可以的,但对于要使用等于的对象,除非您确定要使用 ==(== 表示对象非常具体)

预编辑--

由于某些原因,您的代码无法编译。首先是你的 a 和 b 变量是在 main 范围内声明的,所以它们只能在 main 中访问。将它们移到类中并使其公开和静态意味着可以从任何地方访问它们。

编辑 -- 我不敢相信我正在做某人的作业,但是 -- 这不起作用,但至少它可以编译

import java.util.Random;

public class opgave6 {
    public static Verzameling a = new Verzameling(20, 3);
    public static Verzameling b = new Verzameling(20, 4);

    public static void main(String[] args) {
        System.out.println("before something");
        System.out.println(Verzameling.deelverzamelingVan());
        System.out.println("after something");
    }
}


class Verzameling {
    int[] elementen;
    int elementen2;
    static int aantal2;

    Verzameling(int aantal, int seed) {
        elementen = new int[100];
        int aantal2 = aantal;

        for (int i = 0; i < 100; i++) {
            elementen[i] = i;
        }

        Random random1 = new Random(seed);

        for (int i = 0; i < 100; i++) {
            int r = random1.nextInt(100);
            int temp;
            temp = elementen[i];
            elementen[i] = elementen[r];
            elementen[r] = temp;
        }

        printVerzameling(aantal);
    }

    // you arent using it -- do so or delete it
    Verzameling(int seed) {
    }

    void printVerzameling(int aantal) {
        for (int i = 0; i < aantal; i++) {
            System.out.print(elementen[i] + " ");
        }
        System.out.println();
    }

    static boolean deelverzamelingVan() {
        for (int i = 0; i < aantal2; i++) {
            for (int j = 0; j < aantal2; j++) {
                int aElementen = opgave6.a.elementen[i];
                int bElementen = opgave6.b.elementen[j];
                System.out.println(String.format("Comparing a[i] to b[i] [%d, %d]", aElementen, bElementen));
                if (aElementen == bElementen)
                    break;
            }
        }

        // need to return something, i dont know what.
        return true;
    }


}

【讨论】:

  • 感谢您的评论,但是当我编译时它仍然出错。它说它找不到关于 verzameling.a.elementen、i、verzameling.b.elementen 和 j 的符号。我想我命名错误,可以通过说:classname.objectname.variable of object 来调用变量吗?
  • @javaaaa 如果对象是静态的并且变量是公共的
  • 对象不能是静态的吧?方法 static boolean deelVerzameling 是静态的,但对象不能根据定义是静态的,对吗?
  • 它现在给出了 10 个错误:opgave63.java:64:找不到符号符号:变量 b 位置:类 Verzameling if(Verzameling.a.elementen[i] = Verzameling.b .elementen[j]) ^
  • @javaaaa 您可以在 opgave6 上将 Verzameling a 和 b 设为公共静态属性。我把你的代码放在我的 IDE 中,但它有比这更多的问题......
【解决方案2】:
if(Verzameling.a.elementen[i] = Verzameling.b.elementen[j])

是分配,而不是比较。我想你想要 ==,但更好的选择是覆盖 equals()

if(Verzameling.a.elementen[i] == Verzameling.b.elementen[j])

if(Verzameling.a.elementen[i].equals(Verzameling.b.elementen[j]))

【讨论】:

    【解决方案3】:

    Verzameling.a.elementen[i] = Verzameling.b.elementen[j]

    应该是:

    Verzameling.a.elementen[i] == Verzameling.b.elementen[j]
    

    【讨论】:

    • 感谢您的评论,但是当我编译时它仍然出错。它说它找不到关于 verzameling.a.elementen、i、verzameling.b.elementen 和 j 的符号。我想我命名错误,可以通过说:classname.objectname.variable of object 来调用变量吗?
    【解决方案4】:

    对每个数组进行排序,比较。或者更好的是,将整数放入一个集合类型中,并检查一个是否是另一个的子集。查找 Set 接口,使用 containsAll() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多