Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Removing the first element of args

807603Dec 18 2007 — edited Dec 18 2007
I need to create a method called shift that picks the first data in args and remove it from the array. for example, if you enter on the command line: java DemoShift -a -b -c. The shift method will return "a" and if your print args you will get the output: "bc". I can pick out the first option, but how do you take it out from args?
public class DemoShift
{
	public static void main(String[] args)
	{
		String s = shift(args);
		System.out.println("s = " + s);
		
		for(String t: args) System.out.println(t);
	}

	public static String shift(String[] str)
	{
		ArrayList<String> al = new ArrayList<String>();
		for(String s: str) al.add(s);
		
		String s = al.get(0);
		al.remove(0);
		
		return(s);
	}
}
Thanks for your reply
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 15 2008
Added on Dec 18 2007
7 comments
162 views