Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arraylist

Status
Not open for further replies.

21091958

Programmer
Aug 21, 2005
20
GB
Hi all
I must remove an item and swap an item within the switch statement. I have been working onthis for quite a while without success, any help welcome please!
Thank you

import java.util.ArrayList;
import java.util.List;
import uucPack.InOut;
public class TryList
{ static List theList = new ArrayList();
public static void main(String[] args)
{ while (true)
{ char c = InOut.readChar();
try
{ switch (c)
{ case 'a' :theList.add(getIndex(), getItem());break;
}
}
catch (RuntimeException rte)
{InOut.println("Error during list operation " + rte);
}
}
}
static int getIndex()
{ InOut.print("Enter an index:> ");
return InOut.readInt();
}
static String getItem()
{ InOut.print("Enter an item:> ");
return InOut.readString();
}
}
 
To add an item, use the add() method.
To remove an item, use the remove() method.

To swap an item, remove it, store the item in a temporary variable, and then add it at the required index.

See the API :
--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top