比较2D字符串数组本身

时间:2015-02-21 04:05:36

标签: java arrays intersection

对于我的任务,我需要找到数组的交集并打印出Jaccard索引,这是交集的大小除以两个数组的并集的大小。我需要找出一种比较数组与自身的方法,因为每个数组都是同一个2-D数组的一部分,每个数组的每个元素都是一个包含2个字符的字符串。

对于我的基本输入,我在创建它时使用的示例,2-D数组包含3个相互比较的数组,以及它们自己。打印输出的形式为3x3平方,其中(1,1),(2,2)和(3,3)都将为1,因为它将比较数组与自身。同样,(1,2)和(2,1)是相同的,(2,3)和(3,2)是相同的。

我知道比较可能需要使用嵌套循环,因为2-D迭代。

1 个答案:

答案 0 :(得分:0)

我对您的问题并不是100%肯定,但相同类型的数组不应该难以比较。

Jaccard索引是集合A和集合B的交集大小除以集合A和集合B的大小。我看到它的方式你应该有四个函数,加上一个main函数:

jaccardIndex(array1,array2)

// Get the size of union(array1, array2)
// Get the size of intersect(array1, array2)
// return intersectSize / unionSize (as long as unionSize != 0)

union(array1,array2)

// Create new array to return
// Iterate through array1
// Any element in array1 that isn't in array2 (!isElementOf), add it to the new array
// return the new array

intersect(array1,array2)

// Create new array to return
// Iterate through array1
// Any element that is in array1 that is also in array2(isElementOf), add it to the new array
// return the new array

isElementOf(value,array)

// Iterate through array
// If a given element equals the value, return true

主(参数[])

// Initialize your arrays here,
// Utilize jaccardIndex