I recall seeing, somewhere, an example that stepped through String args[] by deleting the lowest numbered value(s)
public static void main( String args[]) {
while (args.length > 0 ) {
// do something and obliterate elements from args[]
}
}
Obviously, a variable tracking current position in args and compared to args.length will do it; or an ArrayList made from args[]'s contents, with argsAL.size(). Am I mis-remembering an ArrayList example? I know this is a borderline questi开发者_如何学Con, the likely answer is, "No, there isn't and there shouldn't be either!". Maybe I'm over-focused...
Bill
No, there isn't and there shouldn't be either! Deleting from the front of an array would be a needlessly expensive way to do this.
You might be thinking of many scripting languages having a shift
operator to dequeue the next element (perl, bash, etc).
Edit: Just for posterity, here's a pretty simple implementation of Queue that would allow you to "fake" the same functionality (i.e. encapsulate the cursor):
class ArrayQueue<E> extends AbstractQueue<E> {
private int cursor = 0;
private final E[] data;
public ArrayQueue(E[] data) {
this.data = data;
}
private boolean inRange() {
return cursor < data.length;
}
@Override
public E peek() {
return inRange() ? data[cursor] : null;
}
@Override
public E poll() {
return inRange() ? data[cursor++] : null;
}
@Override
public boolean offer(E e) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
cursor = data.length;
}
@Override
public Iterator<E> iterator() {
//ommitted for brevity
}
@Override
public int size() {
return data.length - cursor;
}
}
Usage:
public static void main(String[] args) throws Exception {
Queue<String> argQ = new ArrayQueue<String>(args);
String command = argQ.poll();
String target = argQ.poll();
}
Warning: Untested
You should really use an argument parser. My favorite is Java Simple Argument Parser (JSAP). There is no reason to manipulate the args[] array directly. I use JSAP even for the most basic command line programs, because eventually they aren't "basic" anymore.
If you plan on popping off elements, don't use an array list since the whole array will need to be copied a bunch of times. Instead, do something like this:
public static void main(String args[]) {
for (String arg : args) {
// consume arg somehow
}
}
Just use a for loop, except when you really love arraycopy's.
From a performace view it doesn't matter either, most command lines don't except more than 32k chars and I don't think your app is usually used for 16k args on the command line.
...Am I mis-remembering an ArrayList example?..
Probably or some other programming language.
Some of them have the method shift
which when invoked does that, gives you the 0th element and removes it, something like:
main( args: Array[String) {
name = args.at(0)
args.shift
lastName = args.at(0)
args.shift
}
It would be a weird way to do it. Technically more expensive, but unless you have some crazy big command line, that really isn't even a consideration.
It has been suggested already to use a loop and this can definitely work.
For better control of command line options check out java-getopt.
You should really use a List, perhaps using java.util.Arrays.asList(args)
. Then you have ordered traversal and easy removal of front and back elements.
精彩评论