String[][]删除操作 p[5][2]={{ "fh ", "nh "},{ "mm ", "nh"},{ "gg",''},{ "jj ",''},{ "gh ",''}} 我想去掉{ "gg",''}这一行,又没有现成的方法? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没有, 要自己写.要new一个新的数组, 长度为原来减一 要是你的目的是删除第3行那就这样啊:List list=Arrays.asList(p); list.remove(3); public class test{ public static void main(String[] args) { String[][] p = {{"fh","nh"},{"mm","nh"},{"gg",""},{"jj",""},{"gh",""}}; int count = 0; // 前移 for (int i = 0; i < p.length; i++) { if (p[i][0] == "gg") { for (int j = i + 1; j < p.length; j++) { p[j - 1][0] = p[j][0]; p[j - 1][1] = p[j][1]; } count++; } } //后面的置空 for(int i = p.length - 1;count > 0; count--) { p[i][0] = ""; p[i][1] =""; i--; } for (int i = 0; i < p.length; i++) { for (int j = 0; j < p[i].length; j++) { System.out.print(p[i][j] + " "); } System.out.println(); } }} public class DeleteMemInArray { public static void main (String args[]){ String[][] p={{ "fh ", "nh "},{ "mm ", "nh"},{ "gg",""},{ "jj", ","},{ "gh", ","}}; System.out.print("原数组元素有:"); for (int i=0; i<p.length; i++) for (int j=0; j<p[i].length; j++) System.out.print(p[i][j] +" "); p[2] = null; //删除{ "gg",""} for (int i=2; i<p.length-1; i++) p[i] = p[i+1];//后续数组元素向前移动 System.out.print("\n删除后的数组元素有:"); for (int i=0; i<p.length-1; i++) for (int j=0; j<p[i].length; j++) System.out.print(p[i][j] +" "); }}原数组元素有:fh nh mm nh gg jj , gh , 删除后的数组元素有:fh nh mm nh jj , gh , 其实p[2] = null;是可以省掉的 只要把后面的元素覆盖掉前面的即可 我和4楼的思路一样,不过我新建了一个String[][],将移动后的String赋给它,再循环新的String[][]。这样对不对?我写写看 没错 我原来的代码有个弊端就是原数组的长度不能改变只能在后面动态置空给你的建议是取得删除后原数组“实际”的长度定义一个新的该长度的数组arrayCopy方法即可 String[][]有arrayCopy方法吗/?我最终要得到的还是String[][] public class test{ public static void main(String[] args) { String[][] p = {{"fh","nh"},{"mm","nh"},{"gg",""},{"jj",""},{"gh",""}}; int count = 0; // 前移 for (int i = 0; i < p.length; i++) { if (p[i][0] == "gg") { for (int j = i + 1; j < p.length; j++) { p[j - 1][0] = p[j][0]; p[j - 1][1] = p[j][1]; } count++; } } String [][] pNew = new String[p.length - count][2]; for (int i = 0; i < pNew.length; i++) { // 每一维使用arraycopy System.arraycopy(p[i], 0, pNew[i], 0, 2); } for (int i = 0; i < pNew.length; i++) { for (int j = 0; j < pNew[i].length; j++) { System.out.print(pNew[i][j] + " "); } System.out.println(); } }} 输出:fh nh mm nh jj gh 兄弟,还在问这个问题啊你那个键-值对应,使用String[][]我怎么看怎么不爽啊,你如果不是在学习算法,而真的是在写应用,我看你还是用集合好多了 是 可能是工程的接口设计问题。都需要String[][] 如果设计成 String[][] 这种形式,一般来说应该意味着“只供使用,不需要修改”,否则的话,还是应该用集合。 初学不懂,请高手帮看一下 如何跳过标识不读此行,直接跳过此行? 说说大家去面试java的时候,考官都问了什么问题? java.lang.NoClassDefFoundError高手请进.... 请教一个关于线程的问题 类似delphi 可以用控件迁入一个浏览器,java有没有这样的控件呢? 找一本电子书 一个BYTE的难题! 高分相送 高分赠送----快来!!!! 如何在Session Bean里返回多条记录 发个关于格式化说明符的问题。 关于ArrayList的问题~!!
没有, 要自己写.
要new一个新的数组, 长度为原来减一
list.remove(3);
public class test{
public static void main(String[] args) { String[][] p = {{"fh","nh"},{"mm","nh"},{"gg",""},{"jj",""},{"gh",""}};
int count = 0;
// 前移
for (int i = 0; i < p.length; i++) {
if (p[i][0] == "gg") {
for (int j = i + 1; j < p.length; j++) {
p[j - 1][0] = p[j][0];
p[j - 1][1] = p[j][1];
}
count++;
}
}
//后面的置空
for(int i = p.length - 1;count > 0; count--) {
p[i][0] = "";
p[i][1] ="";
i--;
}
for (int i = 0; i < p.length; i++) {
for (int j = 0; j < p[i].length; j++) {
System.out.print(p[i][j] + " ");
}
System.out.println();
}
}}
public static void main (String args[]){
String[][] p={{ "fh ", "nh "},{ "mm ", "nh"},{ "gg",""},{ "jj", ","},{ "gh", ","}};
System.out.print("原数组元素有:");
for (int i=0; i<p.length; i++)
for (int j=0; j<p[i].length; j++)
System.out.print(p[i][j] +" ");
p[2] = null; //删除{ "gg",""}
for (int i=2; i<p.length-1; i++)
p[i] = p[i+1];//后续数组元素向前移动
System.out.print("\n删除后的数组元素有:");
for (int i=0; i<p.length-1; i++)
for (int j=0; j<p[i].length; j++)
System.out.print(p[i][j] +" ");
}
}
原数组元素有:fh nh mm nh gg jj , gh ,
删除后的数组元素有:fh nh mm nh jj , gh , 其实p[2] = null;是可以省掉的 只要把后面的元素覆盖掉前面的即可
这样对不对?我写写看
只能在后面动态置空给你的建议是取得删除后原数组“实际”的长度
定义一个新的该长度的数组
arrayCopy方法即可
public class test{
public static void main(String[] args) { String[][] p = {{"fh","nh"},{"mm","nh"},{"gg",""},{"jj",""},{"gh",""}};
int count = 0;
// 前移
for (int i = 0; i < p.length; i++) {
if (p[i][0] == "gg") {
for (int j = i + 1; j < p.length; j++) {
p[j - 1][0] = p[j][0];
p[j - 1][1] = p[j][1];
}
count++;
}
}
String [][] pNew = new String[p.length - count][2];
for (int i = 0; i < pNew.length; i++) {
// 每一维使用arraycopy
System.arraycopy(p[i], 0, pNew[i], 0, 2);
}
for (int i = 0; i < pNew.length; i++) {
for (int j = 0; j < pNew[i].length; j++) {
System.out.print(pNew[i][j] + " ");
}
System.out.println();
}
}}
mm nh
jj
gh
你那个键-值对应,使用String[][]我怎么看怎么不爽啊,
你如果不是在学习算法,而真的是在写应用,我看你还是用集合好多了
如果设计成 String[][] 这种形式,一般来说应该意味着“只供使用,不需要修改”,否则的话,还是应该用集合。