We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Truncating an array
Page Index Toggle Pages: 1
Truncating an array (Read 619 times)
Truncating an array
Feb 10th, 2008, 10:18am
 
Is there a way to truncate a big array without copying it to another one?
Cause I get (java.lang.OutOfMemoryError) when doing it.

I tried
Code:
int[] trimmedArray = new int[sp];
System.arraycopy(array, 0, trimmedArray, 0, sp);
return trimmedArray;
but get OutOfMemoryError.

I tried
Code:
array=subset(array, 0, sp);
return array;
but this does the same thing with the same result.

My original array fits in memory, so its annoying not to be able to truncate it.

Re: Truncating an array
Reply #1 - Feb 10th, 2008, 5:35pm
 
I think the answer is - its not possible.  There are some techniques you can do to simulate truncating (nullify all truncated elements), but I'm pretty sure the answer is no.

Could be wrong.

But, to lead you in another direction, could you possibly use an ArrayList or Stack instead?

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html
http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html

Good luck!
Re: Truncating an array
Reply #2 - Feb 11th, 2008, 2:27am
 
You're probably right.
The ArrayList or Vector may help, since they both have a toArray() method.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html
http://www.javacoffeebreak.com/faq/faq0017.html

I'm a Processing/Java beginner so I'm not familiar with Collection, List, ...
but someday I will learn to use them.

I tried this code, Code:
void setup() {
 size(120,120);
 background(153);
 noLoop();
}//setup()

void draw() {
 // Create an instance of class Vector, with initial vector size
 System.out.println("--- input to Vector ---");
 Vector myVector = new Vector(10);
 for (int i=1; i<=3; i++) {
   myVector.addElement( new Integer(i));
   System.out.println(i);
 }
 // Traverse list, printing them all out
 System.out.println("--- output from Vector ---");
 for (Enumeration e = myVector.elements(); e.hasMoreElements();) {
   Integer num = (Integer) e.nextElement();
   System.out.println(num);
 }
 // convert to an array
 System.out.println("--- Vector to Array ---");
 System.out.println("myVector.size() = "+myVector.size());
 Integer[] myArray = new Integer[myVector.size()];
 myVector.toArray(myArray);
 // output array
 System.out.println("--- output from array ---");
 for (int i=0; i<myArray.length; i++) {
   System.out.println(myArray[i]);
 }
 // Traverse list, printing them all out
 System.out.println("--- output from Vector ---");
 for (Enumeration e = myVector.elements(); e.hasMoreElements();) {
   Integer num = (Integer) e.nextElement();
   System.out.println(num);
 }
}//draw()
It's working, but there are 2 problems for my needs:
-- converting the Vector to an array keeps the Vector, so my whole list is now duplicated in memory.
-- the array is an Integer array, it has to be duplicated again by copying it into an int array.
This doesn't work Code:
Integer aa=12;
int bb=(int) aa;
but this works Code:
// to int i from Integer ii
int i = ii.intValue();
// to Integer ii from int i
Integer ii = new Integer( i );
found at http://mindprod.com/jgloss/intvsinteger.html
The more I know, the more I think Java is badly designed from the start, making complex what used to be simple in Pascal or Basic.
Re: Truncating an array
Reply #3 - Feb 11th, 2008, 7:20am
 
I'm not sure if your program even requires generating an array.  I can't really imagine why it would.  So if you can refactor a bit, try solving the problem without using a primitive array at all.

The ArrayList has a method called RemoveRange with which you can truncate the list.  If you're worried about memory, you can call ArrayList.timeToSize() when necessary to cap the amount of memory used to what it currently requires.

To get and set elements on the array list:

// Create a buffer object to store contents
Thing t;

// Load t with the data at index 5
t = (Thing)ArrayList.get(5);

// Perform some operations
t.doSomething();
t.doAnything("Hello");

// Point the same index to the modified object, t
ArrayList.setElementAt(t, 5);
Page Index Toggle Pages: 1