How to make an array of arrays in Java

Hypothetically, I have 5 string array objects:

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

and I want another array object to contain these 5 string arrays. How should I do it? Can I put it in another array?

+82
java arrays
Jan 24 '11 at 10:50
source share
4 answers

Like this:

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

or

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

(The latter syntax can be used in assignments other than the variable declaration point, while the shorter syntax only works with declarations.)

+108
Jan 24 2018-11-11T00:
source share

to try

 String[][] arrays = new String[5][]; 
+54
Jan 24 2018-11-11T00:
source share

While there are two excellent answers telling you how to do this, I feel that the other answer is not enough: in most cases, you should not do this at all.

Arrays are cumbersome, in most cases you better use the collection API .

With the help of collections you can add and remove elements, and for different functions there are specialized collections (index search, sorting, uniqueness, FIFO access, concurrency, etc.).

Although, of course, it’s important and important to know about arrays and their use, in most cases using Collections makes the API more manageable (therefore new libraries like Google Guava are unlikely to use arrays).

So, for your scenario, I would prefer a list of lists, and I would create it using Guava:

 List<List<String>> listOfLists = Lists.newArrayList(); listOfLists.add(Lists.newArrayList("abc","def","ghi")); listOfLists.add(Lists.newArrayList("jkl","mno","pqr")); 
+16
Jan 24 '11 at 11:14
source share

there is a class that I mentioned in the comment we had with Sean Patrick Floyd: I made it with a special use that requires WeakReference, but you can easily change it with any object.

Hoping this one day helps someone :)

 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(); } } } 

Usage example:

 // 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"); } 
+4
Nov 29
source share



All Articles