/** * web开发技术提供 网址: http://www.webkfa.com */ public class Test { /** * Java怎么删除数组中的一个元素并且向前移 * * @param args * @throws IOException */ public static void main(String[] args) { String[] arrays = { "1", "2", "3", "5", "6" }; System.out.println("数组删除前:"); for (int i = 0; i < arrays.length; i++) { System.out.print(arrays[i]+" "); } String[] arrays2 =removeitem(arrays,"2"); System.out.println(""); System.out.println("数组删除后:"); for (int i = 0; i < arrays2.length; i++) { System.out.print(arrays2[i]+" "); } } public static String[] removeitem(String[] arrays,String str){ String[] tempArr = new String[arrays.length]; int i = 0; for(String s:arrays){ if(!s.equals(str)){ tempArr[i] = s; i++; } } return tempArr; } }