no limitan su auto con Integer.MAX_VALUE
aunque esta pregunta se ha hecho hace muchos años, pero ai quería participar con un ejemplo sencillo utilizando sólo Java SE y sin ninguna biblioteca externa
en un primer momento digamos que es teóricamente imposible, pero prácticamente posible
una nueva mirada: si la matriz es un objeto de elementos de lo que trata de tener un objeto que es matriz de matrices
aquí está el ejemplo
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Anosa
*/
public class BigArray<t>{
private final static int ARRAY_LENGTH = 1000000;
public final long length;
private List<t[]> arrays;
public BigArray(long length, Class<t> glasss)
{
this.length = length;
arrays = new ArrayList<>();
setupInnerArrays(glasss);
}
private void setupInnerArrays(Class<t> glasss)
{
long numberOfArrays = length/ARRAY_LENGTH;
long remender = length % ARRAY_LENGTH;
/*
we can use java 8 lambdas and streams:
LongStream.range(0, numberOfArrays).
forEach(i ->
{
arrays.add((t[]) Array.newInstance(glasss, ARRAY_LENGTH));
});
*/
for (int i = 0; i < numberOfArrays; i++)
{
arrays.add((t[]) Array.newInstance(glasss, ARRAY_LENGTH));
}
if (remender > 0)
{
//the remainer will 100% be less than the [ARRAY_LENGTH which is int ] so
//no worries of casting (:
arrays.add((t[]) Array.newInstance(glasss, (int) remender));
}
}
public void put(t value, long index)
{
if (index >= length || index < 0)
{
throw new IndexOutOfBoundsException("out of the reange of the array, your index must be in this range [0, " + length + "]");
}
int indexOfArray = (int) (index/ARRAY_LENGTH);
int indexInArray = (int) (index - (indexOfArray * ARRAY_LENGTH));
arrays.get(indexOfArray)[indexInArray] = value;
}
public t get(long index)
{
if (index >= length || index < 0)
{
throw new IndexOutOfBoundsException("out of the reange of the array, your index must be in this range [0, " + length + "]");
}
int indexOfArray = (int) (index/ARRAY_LENGTH);
int indexInArray = (int) (index - (indexOfArray * ARRAY_LENGTH));
return arrays.get(indexOfArray)[indexInArray];
}
}
y aquí está la prueba
public static void main(String[] args)
{
long length = 60085147514l;
BigArray<String> array = new BigArray<>(length, String.class);
array.put("peace be upon you", 1);
array.put("yes it worj", 1755);
String text = array.get(1755);
System.out.println(text + " i am a string comming from an array ");
}
este código sólo se limita sólo Long.MAX_VALUE
y Java montón pero se puede superar que a medida que quiero (lo hice 3800 MB)
Espero que esto sea útil y brinde una respuesta simple
Simplemente curioso: ¿Por qué necesita mantener esa cantidad de datos en la memoria al mismo tiempo? ¿No sería posible dividir eso en trozos? –
+1 al comentario de bruno. La única forma de tener un archivo completo en la memoria será una ventaja si necesita realizar accesos aleatorios en diferentes puntos del archivo, y en este caso casi con certeza sería mejor analizarlo en una representación más computable – kdgregory
Yo soy Intentando usar un árbol de prefijos (trie) para conservar los datos, esto puede reducirlo lo suficiente como para caber en 2 gb de memoria. –