如何在 Java 中创建数组数组

假设,我有5个字符串数组对象:

String[] array1 = new String[];
String[] array2 = new String[];
String[] array3 = new String[];
String[] array4 = new String[];
String[] array5 = new String[];

我希望另一个数组对象包含这5个字符串数组对象。我该怎么做?我能把它放到另一个数组里吗?

421519 次浏览

试试看

String[][] arrays = new String[5][];

像这样:

String[][] arrays = { array1, array2, array3, array4, array5 };

或者

String[][] arrays = new String[][] { array1, array2, array3, array4, array5 };

(后一种语法可以用在变量声明之外的赋值中,而较短的语法只适用于声明。)

虽然有两个很好的答案告诉你如何去做,但是我觉得还缺少另一个答案: 在大多数情况下,你根本不应该去做。

数组很麻烦,在大多数情况下最好使用 收集 API

使用集合,您可以添加和删除元素,并且有专门用于不同功能的集合(基于索引的查找、排序、唯一性、 FIFO 访问、并发等)。

尽管了解 Ararray 及其用法当然很好也很重要,但在大多数情况下,使用集合使 API 更易于管理(这就是为什么像 谷歌番石榴这样的新库几乎不使用 Ararray)。

因此,对于您的场景,我更喜欢 List,并且我将使用 Guava 创建它:

List<List<String>> listOfLists = Lists.newArrayList();
listOfLists.add(Lists.newArrayList("abc","def","ghi"));
listOfLists.add(Lists.newArrayList("jkl","mno","pqr"));

这就是我在 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<T>
{
private final WeakReference<T>[][] _array;
private final Queue<T> _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<T>();
}


/**
* 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<T>( 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 process
Array2DWeakRefsBuffered<Image> myArray = new Array2DWeakRefsBuffered<Image>(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");
}