如何在2个ArrayLists之间进行差异

时间:2016-10-09 13:45:31

标签: java apache-commons-collection

我正在制作一个网络服务器,可以在java上生成类似棋子的图像。

为检查器建模的类是:

package app.app.image;


import app.app.image.iterate.ImageElementVisitorInterface;
import app.app.image.tiles.Tile;

import java.awt.*;
import java.util.ArrayList;
import org.apache.commons.collections4.CollectionUtils;


/**
 * Created by pcmagas on 5/10/2016.
 *
 * Methos that Implements a Checkerboard with multiple color tiles
 */
public class Checker implements ImageElement
{

    /**
     * The tiles that can be used to Draw the checker
     */
    private ArrayList<Tile> availableTiles=null;

    /**
     * The grid where the tiles are drawn
     */
    private Tile[][] checkerGrid=null;



    /**
     * The checker width
     */
    private int width=0;

    /**
     * The checker Height
     */
    private int height=0;

    private int tileSize=0;

    /**
     * Creates a new Checker
     * @param width the checker's width
     * @param height the checker's height
     */
    public Checker(int width, int height)
    {
        availableTiles= new ArrayList<Tile>();

        this.setHeight(height);
        this.setWidth(width);
    }

    /**
     * @param height The checker's height
     */
    public void setHeight(int height)
    {
       this.height=height;
    }

    /**
     * @return The checker's height
     */
    public int getHeight()
    {
        return this.height;
    }

    /**
     * Seth the tile width
     * @param width
     */
    public void setWidth(int width)
    {
        this.width=width;
    }

    /**
     * Returns how wide is the tile
     * @return
     */
    public int getWidth()
    {
        return this.width;
    }

    /**
     * Method that Allows us to append a tile to the Checker
     */
    public void addTile(Tile t) throws Exception
    {
        if(this.tileSize >0 && t.getSize()!= this.tileSize)
        {
            throw new Exception("The tile does not have the same size with the orthers");
        }
        else if(!this.availableTiles.contains(t))
        {
            this.availableTiles.add(t);
            this.tileSize=t.getSize();
        }
    }


    /**
     * Method that initializes the grid for the checker
     */
    private void initGrid()
    {
        if(this.checkerGrid==null)
        {
            int width = this.width/this.tileSize;
            int height = this.height/this.tileSize;

            this.checkerGrid=new Tile[height][width];
        }
    }

    /**
     * Method that selects a tile during the grid generation
     * @param top The tile that is on top of the current tile
     * @param previous The tile that is on previously from the current tile
     *
     * @return The correctly selected tile
     */
    private Tile selectTile(Tile top,Tile previous)
    {
        ArrayList<Tile> tilesNotToUse=new ArrayList<Tile>();

        if(top==null)
        {
            tilesNotToUse.add(top);
        }

        if(previous==null)
        {
            tilesNotToUse.add(previous);
        }

        //TODO: Create a diff Between this.availableTiles and tilesNotToUse
        return new Tile(Color.black,10,this);
    }

    @Override
    public void draw(ImageElementVisitorInterface visitor)
    {
        this.initGrid();

        for(int i=0;i<this.checkerGrid.length;i++)
        {
            for(int j=0;j<this.checkerGrid[i].length;j++)
            {
                Tile top=(i>0)?this.checkerGrid[i-1][j]:null;
                Tile previous=(j>0)?this.checkerGrid[i][j-1]:null;

                Tile currentTile=this.selectTile(top,previous);
                this.checkerGrid[i][j]= currentTile;

                currentTile.draw(visitor);
            }
        }

    }

}

请对这种方法做一些不知道的事情:

 /**
 * Method that selects a tile during the grid generation
 * @param top The tile that is on top of the current tile
 * @param previous The tile that is on previously from the current tile
 *
 * @return The correctly selected tile
 */
private Tile selectTile(Tile top,Tile previous)
{
    ArrayList<Tile> tilesNotToUse=new ArrayList<Tile>();

    if(top==null)
    {
        tilesNotToUse.add(top);
    }

    if(previous==null)
    {
        tilesNotToUse.add(previous);
    }


    //TODO: Create a diff Between this.availableTiles and tilesNotToUse
    return new Tile(Color.black,10,this);
}

我想在this.availableTiles和tilesNotToUse之间做一个差异。我见过CollectionUtils,但我无法弄清楚如何做到这一点。我想用php http://php.net/manual/en/function.array-diff.php

获得类似的结果

1 个答案:

答案 0 :(得分:2)

您可以使用list1.removeAll(list2); Java Docs因此,您的list1将只包含属于list1的内容而不是list2。如果你愿意,可以反过来。