We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to write a class that contains a 2D array of coordinates. In order to make the function that removes some element specified by its location in the array, i need a function that pops or splices. For 1D arrays I would rather choose for the subset() or arraycopy() function, such as:
EndofNewArrayX = subset(CoordinatesX, Pixel);
BeginofNewArrayX = subset(CoordinatesX, 0, Pixel-1);
NewArrayX = concat(BeginofNewArrayX, EndofNewArrayX);
in which pixel defines the place that needs to be removed.
How can I accomplish this in an 2D array, where the first dimension is targeted to be removed:
int [][]Coordinates = {{5,10}, {6,11}, {14,3},{24,8},{14,10}};
should become for example:
int [][]Coordinates = {{5,10}, {6,11},{24,8},{14,10}};
Would be great if anybody could get me in the right direction.
Answers
This is a terrible idea. Arrays were never designed for this. ArrayLists would be a better choice.
Thank you both for the fast answers, I think koogs is absolutely right and ArrayList would offer a tremendous safe in code lines. I just was not aware of this type of structure and should start thinking more in classes. In this case
Pixel.remove(i)
is sufficient already. Also, the possiblity of using ArrayLists offers synchronization of threads as well. I learned a lot, thanks for the input!In Java (and also JS), variables & functions follow the lowerCaseCamel naming convention.
UpperCaseCamel is reserved for classes & interfaces only. ~O)
Class ArrayList isn't synchronized at all: b-(
http://docs.Oracle.com/javase/8/docs/api/java/util/ArrayList.html
For that, you can pick Collections.synchronizedList() in order to wrap it under:
http://docs.Oracle.com/javase/8/docs/api/java/util/Collections.html#synchronizedList-java.util.List-
Class CopyOnWriteArrayList:
http://docs.Oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArrayList.html
Or even the old deprecated class Vector:
http://docs.Oracle.com/javase/8/docs/api/java/util/Vector.html
As a replacement for
int[][]
, I'd suggest ArrayList<int[]>: O:-)But if you prefer that even the inner dimension not to be an array either, how about IntList in place of that generic
int[]
?: :\">https://Processing.org/reference/IntList.html
And although not
int
type, class PVector is a good fit somehow; given you only need 2 values for the internal dimension: :ar!https://Processing.org/reference/PVector.html
@Melanie --
I would also vote for using an ArrayList of PVectors, as per @GoToLoop 's last suggestion. Unless you have specific performance requirements then this will be very easy to read and understand.
An aside: I'm not sure what you are using these coordinates for, but here is one example use: Because PShape is backed by an immutable array of vertices it is sometimes useful to create a mutable PShape wrapper class using this kind of coordinate ArrayList, then replace the wrapped PShape with a brand new one any time the vertex ArrayList updates (importing the changed ArrayList into the write-once vertices of the new replacement shape).
Thank you all for thinking along!
@GoToLoop when I mentioned synchronization of ArrayLists, I was refering to the possiblity of using Collections.synchronizedList(). However, I just learned about the concept and do now understand that this function can be used for every List and that PVector indeed has internal synchronization. Good to know, thanks!
Basically, I need to destinguish the distance of some cells in an image. Therefore I save high intensity pixels of an image in a list as part of the object Cell. With those pixels I want to:
Therefore, I should have easy acces to the pixels:
Concluding, if I understand correctly:
I think I'll go for the ArrayList type, thanks for clearing up the different List options!
Processing's PVector class got no synchronization at all.
And got nothing to do w/ Java's Vector class either: [-X
Oh this doesn't work out too nice, I'm sorry. My < Object > and < IntList > dissapeared, since I didnt not use the space in the comment above...
What I meant was the difference between ArrayList < IntList > and ArrayList < Object > Thus, with ArrayList < Object > I could add the loc = x * y * width once within the class and not during every new ArrayList < IntList > item. Right?
Just some an alternative approach. How about a class to represent those coords and their corresponding converted index? :D
Later, you can place them in an ArrayList container. Check it out below: O:-)