Why isn't there a remove method for an array?

Pointless question (unless I'm missing something), but why isn't there a method remove(int index) for an Array?I know ways around it, but it just surprises me that it doesn't exist, I'm guessing there's a good reason for this?
[245 byte] By [abu5ea] at [2007-11-26 19:21:15]
# 1
What do you mean an Array? Are you talking about an ArrayList?
zadoka at 2007-7-9 21:40:22 > top of Java-index,Java Essentials,Java Programming...
# 2
So what are the array methods, anyway, besides the ones defined in Object?
DrLaszloJamfa at 2007-7-9 21:40:22 > top of Java-index,Java Essentials,Java Programming...
# 3

No, an array, for example, why do I have to do this?:

for (int i = 0; i < bufferIndex-1; i++) {

buffer[i] = buffer[i + 1];

}

To remove the first element? (short of holding two indices)

abu5ea at 2007-7-9 21:40:22 > top of Java-index,Java Essentials,Java Programming...
# 4
> Pointless question (unless I'm missing something),> but why isn't there a method remove(int index) for an> Array?java.lang.reflect.Array?java.sql.Array?~
yawmarka at 2007-7-9 21:40:22 > top of Java-index,Java Essentials,Java Programming...
# 5
binarySearch, fill, sort (unless these are overridden from object?)
abu5ea at 2007-7-9 21:40:23 > top of Java-index,Java Essentials,Java Programming...
# 6
> No, an array...Arrays in Java are not resizeable.[url= http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html]The Java?Tutorial - Arrays[/url]~
yawmarka at 2007-7-9 21:40:23 > top of Java-index,Java Essentials,Java Programming...
# 7
> binarySearch, fill, sort (unless these are overridden> from object?)Those methods are in java.util.Arrays. ~
yawmarka at 2007-7-9 21:40:23 > top of Java-index,Java Essentials,Java Programming...
# 8
Yeah I understand that much, but I have a method that has a fixed length array, implemented as queue, so when the first element is "removed", I'd want all the other elements shifted to the left.
abu5ea at 2007-7-9 21:40:23 > top of Java-index,Java Essentials,Java Programming...
# 9
I know this could be done with an arrayList, and I understand the method doesn't exist, but am I the only one who thinks it would be useful?
abu5ea at 2007-7-9 21:40:23 > top of Java-index,Java Essentials,Java Programming...
# 10

> Yeah I understand that much, but I have a method that

> has a fixed length array, implemented as queue, so

> when the first element is "removed", I'd want all the

> other elements shifted to the left.

You'll need to write the code to "shift left" yourself, or use an existing implementation of a queue (e.g., [url=http://java.sun.com/javase/6/docs/api/java/util/LinkedList.html]java.util.LinkedList[/url]).

> am I the only one who thinks it would be useful?

You just need to get more familiar with the existing API...

~

yawmarka at 2007-7-9 21:40:23 > top of Java-index,Java Essentials,Java Programming...
# 11

> I know this could be done with an arrayList, and I

> understand the method doesn't exist, but am I the

> only one who thinks it would be useful?

You're probably not the only one, but the ones who want that are using a simple array for the wrong reason. They should instead be using a dynamic collection such as ArrayList or LinkedList, etc.

warnerjaa at 2007-7-9 21:40:23 > top of Java-index,Java Essentials,Java Programming...
# 12
If there is a fixed maximum queue length, then it sounds like a data structurethat can easier be implemented using a [url= http://en.wikipedia.org/wiki/Circular_queue]Circular Buffer[/url].
DrLaszloJamfa at 2007-7-9 21:40:23 > top of Java-index,Java Essentials,Java Programming...
# 13
Yeah good point, I'll just implement an arrayList instead, the extra line for size will be better than having a loop. Cheers.
abu5ea at 2007-7-9 21:40:23 > top of Java-index,Java Essentials,Java Programming...
# 14

actually saying that, the array I am using is a byte array, which works well for splitting strings, and to convert I would have to use a wrapper class, so maybe using this loop is the best idea?

I want to keep this as simple as possible, so I'm going to shy away from writing a circular buffer, thanks anyway.

abu5ea at 2007-7-9 21:40:23 > top of Java-index,Java Essentials,Java Programming...
# 15
> am I the only one who thinks it would be useful?I just had to post this when I thought again at the above question. It kind of fits the situation :-D http://www.despair.com/loneliness.html
warnerjaa at 2007-7-21 17:33:49 > top of Java-index,Java Essentials,Java Programming...
# 16

> Yeah I understand that much, but I have a method that

> has a fixed length array, implemented as queue, so

> when the first element is "removed", I'd want all the

> other elements shifted to the left.

The easiest way to implement a queue in an array is using a circular queue. Removing the first element amounts to changing an index; structural modification of the array is unnecessary.

Edit: ..... just like DrLaszloJamf pointed out.

jsalonena at 2007-7-21 17:33:49 > top of Java-index,Java Essentials,Java Programming...