Java 从新数组中的2个数组中移动非重复数字

Java 从新数组中的2个数组中移动非重复数字,java,arrays,for-loop,if-statement,Java,Arrays,For Loop,If Statement,我希望第一个数组中与第二个数组中的数字不重复的数字转到第三个数组 这是我到现在为止所做的,但它不起作用…请帮助我 for(int i = 0; i < isir.length; i++) { for(int j = 0 ; j < isir2.length; j++) { if(isir[i] != isir[j]) { for(int k = 0; k < sirdif1.length; k++)

我希望第一个数组中与第二个数组中的数字不重复的数字转到第三个数组

这是我到现在为止所做的,但它不起作用…请帮助我

for(int i = 0; i < isir.length; i++) 
{
    for(int j = 0 ; j < isir2.length; j++) 
    {
        if(isir[i] != isir[j]) 
        {
            for(int k = 0; k < sirdif1.length; k++) 
            {
                sirdif1[k] = isir[i];
            }
        }
    }
}
for(int i=0;i

我正在使用扫描器功能从控制台输入数字…

我建议首先使用好的命名约定。
sirdif1
之类的变量(我甚至无法想象这个名称是如何确定的…)确实让其他人难以阅读/帮助

int a1[] ; // Initialize array1
int a2[] ; // Initialize array2

List<Integer> tempList = new ArrayList<Integer>();
for(int i = 0; i < a2.length; i++)
{
    boolean found = false; // To be able to track if the int was found in both arrays
    for(int j = 0; j < a1.length; j++)
    {
        if(a1[j].equals(a2[i]))
        {
            found = true; 
            break; // If it exist in both arrays there is no need to look further
        }
    }
    if(!found ) // If the same is not found in both..
        tempList.add(a2[i]); // .. add to temporary list
}
int a1[];//初始化数组1
int a2[];//初始化数组2
List templast=new ArrayList();
对于(int i=0;i
使用列表和蒸汽:

    Integer a1[] = {1,2,5,6,8};
    Integer a2[] = {1,3,5,7,8};

    List<Integer> result = new ArrayList<>();

    // Add elements from first array which ist not in the second
    Arrays.stream(a1).filter(_a -> !Arrays.asList(a2).contains(_a)).forEach(result::add);
    // Add elements from second array which ist not in the first
    Arrays.stream(a2).filter(_a -> !Arrays.asList(a1).contains(_a)).forEach(result::add);
    result.forEach(System.out::println);
整数a1[]={1,2,5,6,8};
整数a2[]={1,3,5,7,8};
列表结果=新建ArrayList();
//添加第一个数组中不在第二个数组中的元素
Arrays.stream(a1).filter(_a->!Arrays.asList(a2).contains(_a)).forEach(result::add);
//添加第二个数组中不在第一个数组中的元素
Arrays.stream(a2).filter(_a->!Arrays.asList(a1).contains(_a)).forEach(result::add);
result.forEach(System.out::println);
输出将是: 2. 6. 3.
7

为了清晰起见,我可能会使用
Set
s

public void test() {
    Integer[] a1 = {1,2,3,4,5};
    Integer[] a2 = {2,3,4};
    // Treat them as Sets.
    Set<Integer> s1 = new HashSet<>(Arrays.asList(a1));
    Set<Integer> s2 = new HashSet<>(Arrays.asList(a2));
    // Get the union of both.
    Set<Integer> all = new HashSet<>(s1);
    all.addAll(s2);
    // Find the repeating ones.
    Set<Integer> common = new HashSet<>(s1);
    common.retainAll(s2);
    // Non-repeating is.
    Set<Integer> nonRepeats = new HashSet<>(all);
    nonRepeats.removeAll(common);
    System.out.println(nonRepeats);

}
公共无效测试(){
整数[]a1={1,2,3,4,5};
整数[]a2={2,3,4};
//把它们当作一套。
Set s1=新的HashSet(Arrays.asList(a1));
Set s2=新的HashSet(Arrays.asList(a2));
//把两者结合起来。
Set all=新哈希集(s1);
all.addAll(s2);
//找到重复的。
Set common=新哈希集(s1);
普通。保留(s2);
//不重复的是。
Set nonRepeats=新哈希集(全部);
非重复。移除所有(普通);
系统输出打印项次(非重复);
}

按照目前的编写方式,很难准确说出您的要求。请参阅页面以获取澄清此问题的帮助。
public void test() {
    Integer[] a1 = {1,2,3,4,5};
    Integer[] a2 = {2,3,4};
    // Treat them as Sets.
    Set<Integer> s1 = new HashSet<>(Arrays.asList(a1));
    Set<Integer> s2 = new HashSet<>(Arrays.asList(a2));
    // Get the union of both.
    Set<Integer> all = new HashSet<>(s1);
    all.addAll(s2);
    // Find the repeating ones.
    Set<Integer> common = new HashSet<>(s1);
    common.retainAll(s2);
    // Non-repeating is.
    Set<Integer> nonRepeats = new HashSet<>(all);
    nonRepeats.removeAll(common);
    System.out.println(nonRepeats);

}