java如何制作简单的数组_如何用Java制作数组的数组

bd02fb8a85de4636967948c4881d025f.png

摇曳的蔷薇

在与肖恩·帕特里克·弗洛伊德(Sean Patrick Floyd)一起发表的评论中,我提到了一个类:我使用了特殊的用法,它需要WeakReference,但您可以通过任何对象轻松地对其进行更改。希望有一天能对某人有所帮助:)import java.lang.ref.WeakReference;import java.util.LinkedList;import java.util.NoSuchElementException;import java.util.Queue;/** * * @author leBenj */public class Array2DWeakRefsBuffered{    private final WeakReference[][] _array;    private final Queue _buffer;    private final int _width;    private final int _height;    private final int _bufferSize;    @SuppressWarnings( "unchecked" )    public Array2DWeakRefsBuffered( int w , int h , int bufferSize )    {        _width = w;        _height = h;        _bufferSize = bufferSize;        _array = new WeakReference[_width][_height];        _buffer = new LinkedList();    }    /**     * Tests the existence of the encapsulated object     * /!\ This DOES NOT ensure that the object will be available on next call !     * @param x     * @param y     * @return     * @throws IndexOutOfBoundsException     */public boolean exists( int x , int y ) throws IndexOutOfBoundsException    {        if( x >= _width || x < 0 )        {            throw new IndexOutOfBoundsException( "Index out of bounds (get) : [ x = " + x + "]" );        }        if( y >= _height || y < 0 )        {            throw new IndexOutOfBoundsException( "Index out of bounds (get) : [ y = " + y + "]" );        }        if( _array[x][y] != null )        {            T elem = _array[x][y].get();            if( elem != null )            {            return true;            }        }        return false;    }    /**     * Gets the encapsulated object     * @param x     * @param y     * @return     * @throws IndexOutOfBoundsException     * @throws NoSuchElementException     */    public T get( int x , int y ) throws IndexOutOfBoundsException , NoSuchElementException    {        T retour = null;        if( x >= _width || x < 0 )        {            throw new IndexOutOfBoundsException( "Index out of bounds (get) : [ x = " + x + "]" );        }        if( y >= _height || y < 0 )        {            throw new IndexOutOfBoundsException( "Index out of bounds (get) : [ y = " + y + "]" );        }        if( _array[x][y] != null )        {            retour = _array[x][y].get();            if( retour == null )            {            throw new NoSuchElementException( "Dereferenced WeakReference element at [ " + x + " ; " + y + "]" );            }        }        else        {            throw new NoSuchElementException( "No WeakReference element at [ " + x + " ; " + y + "]" );        }        return retour;    }    /**     * Add/replace an object     * @param o     * @param x     * @param y     * @throws IndexOutOfBoundsException     */    public void set( T o , int x , int y ) throws IndexOutOfBoundsException    {        if( x >= _width || x < 0 )        {            throw new IndexOutOfBoundsException( "Index out of bounds (set) : [ x = " + x + "]" );        }        if( y >= _height || y < 0 )        {            throw new IndexOutOfBoundsException( "Index out of bounds (set) : [ y = " + y + "]" );        }        _array[x][y] = new WeakReference( o );        // store local "visible" references : avoids deletion, works in FIFO mode        _buffer.add( o );        if(_buffer.size() > _bufferSize)        {            _buffer.poll();        }    }}使用方法示例:// a 5x5 array, with at most 10 elements "bufferized" -> the last 10 elements will not be taken by GC processArray2DWeakRefsBuffered myArray = new Array2DWeakRefsBuffered(5,5,10);Image img = myArray.set(anImage,0,0);if(myArray.exists(3,3)){    System.out.println("Image at 3,3 is still in memory");}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值