如何在Java中创build一个数组的数组
假设我有5个string数组对象:
String[] array1 = new String[]; String[] array2 = new String[]; String[] array3 = new String[]; String[] array4 = new String[]; String[] array5 = new String[];
我想另一个数组对象包含这5个string数组对象。 我该怎么做? 我可以把它放在另一个数组中吗?
喜欢这个:
String[][] arrays = { array1, array2, array3, array4, array5 };
要么
String[][] arrays = new String[][] { array1, array2, array3, array4, array5 };
(后面的语法可以在variables声明之外的赋值中使用,而较短的语法只能用于声明。)
尝试
String[][] arrays = new String[5][];
虽然有两个很好的答案告诉你如何做到这一点,但我觉得还缺less另一个答案:在大多数情况下,你不应该这样做。
数组很麻烦,在大多数情况下,您最好使用Collection API 。
使用集合,您可以添加和删除元素,并为不同的function(基于索引的查找,sorting,唯一性,FIFO访问,并发等)提供专用的集合。
虽然了解数组及其使用方法当然很重要,但在大多数情况下,使用Collections可以使API更易于pipe理(这就是为什么像Google Guava这样的新库几乎不使用数组)。
所以,对于你的情况,我宁愿列表清单,我会使用番石榴创build它:
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"); }