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 › splice in Arrays
Page Index Toggle Pages: 1
splice in Arrays (Read 1071 times)
splice in Arrays
Nov 6th, 2009, 3:19pm
 
Hello,

This may be such a simple question but I can't seem to find the answer in the Discourse or documentation.

I am just trying to pop out an element form an array but I can't use shorten() because I want to extract an element from the middle and I can't use subset() because it doesn't change the source array and I don't only want that element, I want to get it out from the array so I can repeat the process.

In a nutshell this is what I want to do:

int[] count = {1, 2, 3};
/***** some sort of function to take out the number 2, like *****/
count = remove(count, 1, 1);

print(count); // Prints "1" "3"

Sort of like the splice() method in Actionscript where you can take out elements from an array by providing the index and count of elements to be deleted.

subset() gives me the elements I want but it doesn't modify the original array, I want to get the opposite of subset().

I guess I could subset the first half, and then append the second half with another subset to the same array but there has to be a direct way, like a method I am just not finding.

Thanks you in advance!

Fernando Toledo
Re: splice in Arrays
Reply #1 - Nov 6th, 2009, 3:29pm
 
you should use arraylist insteadt of an array http://processing.org/reference/ArrayList.html

you can use remove then http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html#remove%28java.lang.Object%29
Re: splice in Arrays
Reply #2 - Nov 6th, 2009, 3:47pm
 
Hey Cedric,

Thank you for the quick response.

I am using an ArrayList now however I ran into a little problem, since the ArrayList is an object, I can't use it's get method to pass an int into a class. How could I datatype what myArrayList.get(0) returns into an int?

Thank you!
Re: splice in Arrays
Reply #3 - Nov 6th, 2009, 4:10pm
 
Good question, i never used it myself...
i just did some quick research and found the following

Quote:
The generics facility in Java can be used only for
object (reference) types.  It cannot be used primitive
types like 'int' or 'float'.

So, if you wanted an ArrayList that contained only
integer data, then you'd need to use the object type
which corresponds to int: java.lang.Integer.

  ArrayList<Integer> numbers;
  numbers = new ArrayList<Integer>(40);

Fortunately, the new Autobox/unbox features of Java can
make this work just like an ArrayList of int.

  numbers.add(27);

The value 27 will be automatically boxed as an Integer
object, then added to the ArrayList<Integer>.

For more information, consult
http://java.sun.com/developer/technicalArticles/J2SE/generics/index.html
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf


but this seems not to work as stated in this post asking the same question.
http://processing.org/discourse/yabb2/num_1187580155.html
So i just dont know what would be a good way. But i am sure PhiLho knows what to do Smiley

another way would probably be to store the ints in the object but maybe not the best.

Re: splice in Arrays
Reply #4 - Nov 6th, 2009, 6:06pm
 
True, it's not working for me either... Lot's if syntax errors heh.

Anyway in the end I separated it into 2 processes, it seems silly but it works, I basically take the first half, then the second half minus the middle element and then concatenate them together  Undecided

Like this:

Code:

// Instantiate the array
int[] myCountingArray = new int[100];

// Just as an example I'll populate
// the array with numbers from 1 to 100
for (int i = 0; i < 100; i++){
 myCountingArray[i] = i + 1;
}

// then let's say I want to get the middle element
// instantiate two temp arrays
int[] firstHalf = new int[100];
int[] secondHalf = new int[100];

// subset the first half of the array into the firstHalf array
firstHalf = subset(myCountingArray, 0, round(myCountingArray.length / 2));

// subset the second half (minus the middle element) to the secondHalf array
secondHalf = subset(myCountingArray, round(myCountingArray.length / 2) + 1);

// concatenate both
myCountingArray = concat(firstHalf, secondHalf);


The same process can be applied to extract any element really, just instead of using the length/2 it would be replaced with the index of the element we want to extract - 1 on the first subset and then the index on the second subset...

I'm not saying I reinvented the wheel, I'm just merely trying to point out that what I did seems silly but it's the only way I found to do a simple task as removing a desired element from an array (that isn't the last element).

There's probably a quicker and more straightforward solution, hope anyone can share it.
Re: splice in Arrays
Reply #5 - Nov 7th, 2009, 5:37am
 
When you use ArrayList (or java.util.Vector or anything similar) with ints or floats, you do:

int i = 100;
ArrayList<Integer> al;
al.add(new Integer(i));
i = al.get(0).intValue();

(or new Float(x) and floatValue() etc.)

Your approach with using two temporary arrays could be optimized it little by doing this:
Code:

float numbers[];
int index = 15; // index of element to remove
float removedNumber = numbers[index];
System.arraycopy(numbers, index + 1, numbers, index, numbers.length - index - 1);

This shifts the second half of your array left by one element (and only copies one half of the array instead of copying both halfs as in your solution).  However, now numbers.length are not the number of "valid" elements in the array anymore (numbers is technically still the same size after the arraycopy, even though you logically removed one element).  You'd have to define a variable int N or something to keep track of the "logical length" of your array.
Re: splice in Arrays
Reply #6 - Nov 9th, 2009, 12:28pm
 
Quote:
When you use ArrayList (or java.util.Vector or anything similar) with ints or floats, you do:

int i = 100;
ArrayList<Integer> al;
al.add(new Integer(i));
i = al.get(0).intValue();

(or new Float(x) and floatValue() etc.)


Thank you! Using this approach I can use the remove() method directly in the ArrayList and still be able to cast an int value into a custom class

Smiley
Page Index Toggle Pages: 1