【问题标题】:How to make a diff between 2 ArrayLists如何在 2 个 ArrayLists 之间进行区分
【发布时间】:2016-10-09 13:45:31
【问题描述】:

我正在制作一个网络服务器,可以在 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实现类似的结果

【问题讨论】:

  • 你的问题的标题是“随机生成器应该是方法内部的局部变量还是实例变量?”但我在您的问题中看不到任何与随机生成器相关的内容。怎么会?
  • 它来自以前的非问问题。感谢您指出,

标签: java apache-commons-collection


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 2011-04-01
    • 1970-01-01
    • 2017-12-19
    • 2016-01-28
    相关资源
    最近更新 更多