如何删除二维数组中的特定元素(40分) 如我有一个二维数组a={{1,1,2,2,0,0,0,0},{3,3,4,4,5,5,0,0},{-1,-1,-2,-2,-3,-3,-4,-4},{2,2,0,0,0,0,0,0}};怎么才力吧里面的0删除掉变成 a={{1,1,2,2,},{3,3,4,4,5,5},{-1,-1,-2,-2,-3,-3,-4,-4,},{2,2,}};请各位高手说的详细点啊~~我是新手~~先在这谢谢了!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 2个for循环, if([i][j]==0) [i][j]==null; 得到不规则的2维数组。 java允许数组长度不一样的比如int[][] aaa = new int[2][];aaa[0] = new int[3];aaa[0][0] = 0;aaa[0][1] = 1;aaa[0][2] = 2;aaa[1] = new int[2];aaa[1][0] = 0;aaa[1][1] = 1; 上边的在开玩笑吗?[i][j]==null? 已经创建好的数组不能实现。除非重新定义,java中支持不同长度的二维数组。 首先你得确认,你定义的不是int型数组。而是Integer因为int是原始数据类型不可能赋null值。for (int j=0,z=0; j < i.length ;z++) { if(z >= i[j].length) { z=0; j++; } if(j < i.length) if(i[j][z] == 0) i[j][z] = null; } int[][] a={{1,1,2,2,0,0,0,0}, {3,3,4,4,5,5,0,0}, {-1,-1,-2,-2,-3,-3,-4,-4}, {2,2,0,0,0,0,0,0}, {0,0,0,0,0}}; //将非零元素导入到容器里 LinkedList<LinkedList<Integer>> b = new LinkedList<LinkedList<Integer>>(); for(int i=0;i<a.length;i++) { LinkedList<Integer> bchild = new LinkedList<Integer>(); for(int j=0;j<a[i].length;j++) { if(0!=a[i][j]) bchild.add(a[i][j]); } if(!bchild.isEmpty()) b.add(bchild); } //将容器里的元素导入到数组里 a = new int[b.size()][]; Iterator<LinkedList<Integer>> ite = b.iterator(); int index = 0; while(ite.hasNext()) { LinkedList<Integer> lchild = ite.next(); a[index] = new int[lchild.size()]; Iterator<Integer> iteChild = lchild.iterator(); int indexChild = 0; while(iteChild.hasNext()) { a[index][indexChild] = iteChild.next(); indexChild ++; } index++; } //测试 System.out.println(a.length);好麻烦啊,暂时没有想到更好的办法,楼主非要这样么,看看能不能改下设计啊 我是用数组实现的,楼主请查阅:public class DeleteZero { private static int[][] intArray = { {0,1,1,0,2,2,0,0,0,0}, {0,3,3,4,0,4,5,5,0,0}, {0,-1,-1,0,-2,-2,-3,-3,-4,-40,}, {0,2,0,2,0,0,0,0,0,0}, {0}, {1} }; private static int[] temp, target; //temp是过渡数组,target存储非零元素 private static int[][] result; private static int count = 0; public static int[][] deletZero(int[][] intArray){ int firstDimesion = intArray.length; //定义二维数组的第一维的长度 for(int i=0; i<intArray.length; i++){ if(intArray[i].length == 1 && intArray[i][0] == 0) firstDimesion --; } result = new int[firstDimesion][]; firstDimesion = 0; for(int i=0; i<intArray.length; i++){ if(intArray[i].length == 1 && intArray[i][0] == 0) continue; temp = intArray[i]; target = temp; for(int j=0; j<temp.length; j++){ if(temp[j] != 0){ target[count] = temp[j]; count ++; } } result[firstDimesion] = new int[count]; for(int k=0; k<count; k++) result[firstDimesion][k] = target[k]; count = 0; //count置0 firstDimesion ++; //长度增一,初始化下一个数组 } return result; } public static void printArray(int[][] intArray){ for(int i=0; i<intArray.length; i++){ for(int j=0; j<intArray[i].length; j++) System.out.print(intArray[i][j] +" "); System.out.println(); } } public static void main(String args[]){ deletZero(intArray); printArray(result); }}输出:1 1 2 2 3 3 4 4 5 5 -1 -1 -2 -2 -3 -3 -4 -40 2 2 1 纠正一点小错误:deletZero(int[][] intArray)其实返回类型可以是void,因为该方法的操作对象result是静态的,当然了,结果没有什么影响。 bchild.add(a[i][j]); 这句不对啊!!! 我上面确实说错了,java运行长度不同的二维数组,我用jdk1.4版本写的package test;public class Test{ public static void main( String[] args ) { int[][] a = { {1,1,2,2,0,0,0,0}, {3,3,4,4,5,5,0,0}, {-1,-1,-2,-2,-3,-3,-4,-4}, {2,2,0,0,0,0,0,0} }; int[][] result = new int[a.length][]; for( int i=0; i<a.length; i++ ) { //算出当前第二维数组的长度 int size = 0; for( int j=0; j<a[i].length; j++ ) { if(a[i][j]!=0) size++; } //复制非0数据 result[i] = new int[size]; int pos = 0; for( int j=0; j<a[i].length; j++ ) { if(a[i][j]!=0) { result[i][pos] = a[i][j]; pos++; } } } //打印结果 for( int i=0; i<result.length; i++ ) { for( int j=0; j<result[i].length; j++ ) { System.out.print( result[i][j] + "," ); } System.out.println(); } }} 你的jdk是不是版本太低没办法自动装箱啊 import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;public class TestArr { public static void main(String args[]){ int a[][] ={{1,1,2,2,0,0,0,0},{3,3,4,4,5,5,0,0},{-1,-1,-2,-2,-3,-3,-4,-4},{2,2,0,0,0,0,0,0}}; List<Integer> list = new ArrayList<Integer>(); Map<Integer,List> map = new HashMap<Integer,List>(); for(int i = 0; i < a.length; i++){ for(int j = 0; j < a[i].length; j++){ if(a[i][j] == 0) continue; else{ list.add(a[i][j]); } } map.put(i, list); list = new ArrayList<Integer>(); } int b[][] = new int[a.length][]; Iterator it = map.entrySet().iterator(); for(int i = 0; i < a.length; i++){ Map.Entry entry =(Map.Entry)it.next(); b[i] = new int[((List)entry.getValue()).size()]; for(int j = 0; j < ((List)entry.getValue()).size(); j++) b[i][j] = (Integer)((List)entry.getValue()).get(j); } for(int i = 0; i < b.length; i++){ for(int j = 0; j < b[i].length; j++) System.out.print(" " + b[i][j]); System.out.println(); } }} 楼上的,数组a中加一个{0,0,0}元素,用for(int i = 0;i<b.length;i++) { System.out.println(Arrays.toString(b[i])); }打印会发现有空数组啊 就是这样 public static void main(String[] args) { int[][] a = { { 1, 1, 2, 2, 0, 0, 0, 0 }, { 3, 3, 4, 4, 5, 5, 0, 0 }, { -1, -1, -2, -2, -3, -3, -4, -4 }, { 2, 2, 0, 0, 0, 0, 0, 0 } }; List<List> list = new ArrayList<List>(); for (int i = 0; i < a.length; i++) { List t = new ArrayList(); for (int j = 0; j < a[i].length; j++) { if (a[i][j] != 0) { t.add(a[i][j]); } } list.add(t); } System.out.println(list); } 虽然楼主已经结贴了,还是贴一下改进后的代码package test;import java.util.ArrayList;public class Test{ public static int[][] deleteZero( int[][] a ) { ArrayList list = new ArrayList(); for( int i=0; i<a.length; i++ ) { //算出当前第二维数组的长度 int size = 0; for( int j=0; j<a[i].length; j++ ) { if(a[i][j]!=0) size++; } //如果全部为0,跳过 if( size==0 ) continue; //复制非0数据 int[] array = new int[size]; int pos = 0; for( int j=0; j<a[i].length; j++ ) { if(a[i][j]!=0) { array[pos] = a[i][j]; pos++; } } list.add( array ); } //生成结果数组 int[][] result = new int[list.size()][]; for( int i=0; i<result.length; i++ ) result[i] = (int[])list.get( i ); return result; } public static void main( String[] args ) { int[][] a = { {1,1,2,2,0,0,0,0}, {3,3,4,4,5,5,0,0}, {-1,-1,-2,-2,-3,-3,-4,-4}, {0,0,0,0,0,0,0,0} }; int[][] result = deleteZero( a ); //打印结果 for( int i=0; i<result.length; i++ ) { for( int j=0; j<result[i].length; j++ ) { System.out.print( result[i][j] + "," ); } System.out.println(); } }} java初学者:java中怎样可以知道一个音乐文件的播放时长? 如何移除JMenubar 怎么让一个线程等待多次? java网络问题!不是很难,帮着看看吧!(2) 对象和类,精通的人会在潜意识中区别她,而不会的人觉得很难理解 写程序毫无章法,结构混乱,程序臃肿,怎么办? 如何使用socket,在C/S程序之间传递数组? 怎样能把JTable设成只读的,有谁知道吗?我很急用啊 各位大侠,那里可以找到Inprise Application Server class类文件直接修改反编译 锁的同步小问题 继承JPanel类的定义
比如
int[][] aaa = new int[2][];
aaa[0] = new int[3];
aaa[0][0] = 0;
aaa[0][1] = 1;
aaa[0][2] = 2;
aaa[1] = new int[2];
aaa[1][0] = 0;
aaa[1][1] = 1;
首先你得确认,你定义的不是int型数组。而是Integer因为int是原始数据类型不可能赋null值。for (int j=0,z=0; j < i.length ;z++) {
if(z >= i[j].length) {
z=0;
j++;
}
if(j < i.length)
if(i[j][z] == 0)
i[j][z] = null;
}
{3,3,4,4,5,5,0,0},
{-1,-1,-2,-2,-3,-3,-4,-4},
{2,2,0,0,0,0,0,0},
{0,0,0,0,0}};
//将非零元素导入到容器里
LinkedList<LinkedList<Integer>> b = new LinkedList<LinkedList<Integer>>();
for(int i=0;i<a.length;i++)
{
LinkedList<Integer> bchild = new LinkedList<Integer>();
for(int j=0;j<a[i].length;j++)
{
if(0!=a[i][j])
bchild.add(a[i][j]);
}
if(!bchild.isEmpty())
b.add(bchild);
}
//将容器里的元素导入到数组里
a = new int[b.size()][];
Iterator<LinkedList<Integer>> ite = b.iterator();
int index = 0;
while(ite.hasNext())
{
LinkedList<Integer> lchild = ite.next();
a[index] = new int[lchild.size()];
Iterator<Integer> iteChild = lchild.iterator();
int indexChild = 0;
while(iteChild.hasNext())
{
a[index][indexChild] = iteChild.next();
indexChild ++;
}
index++;
}
//测试
System.out.println(a.length);
好麻烦啊,暂时没有想到更好的办法,楼主非要这样么,看看能不能改下设计啊
private static int[][] intArray = {
{0,1,1,0,2,2,0,0,0,0},
{0,3,3,4,0,4,5,5,0,0},
{0,-1,-1,0,-2,-2,-3,-3,-4,-40,},
{0,2,0,2,0,0,0,0,0,0},
{0},
{1}
};
private static int[] temp, target; //temp是过渡数组,target存储非零元素
private static int[][] result;
private static int count = 0;
public static int[][] deletZero(int[][] intArray){
int firstDimesion = intArray.length; //定义二维数组的第一维的长度
for(int i=0; i<intArray.length; i++){
if(intArray[i].length == 1 && intArray[i][0] == 0)
firstDimesion --;
}
result = new int[firstDimesion][];
firstDimesion = 0;
for(int i=0; i<intArray.length; i++){
if(intArray[i].length == 1 && intArray[i][0] == 0)
continue;
temp = intArray[i];
target = temp;
for(int j=0; j<temp.length; j++){
if(temp[j] != 0){
target[count] = temp[j];
count ++;
}
}
result[firstDimesion] = new int[count];
for(int k=0; k<count; k++)
result[firstDimesion][k] = target[k]; count = 0; //count置0
firstDimesion ++; //长度增一,初始化下一个数组
}
return result;
}
public static void printArray(int[][] intArray){
for(int i=0; i<intArray.length; i++){
for(int j=0; j<intArray[i].length; j++)
System.out.print(intArray[i][j] +" ");
System.out.println();
}
}
public static void main(String args[]){
deletZero(intArray);
printArray(result);
}
}输出:
1 1 2 2
3 3 4 4 5 5
-1 -1 -2 -2 -3 -3 -4 -40
2 2
1
{
public static void main( String[] args )
{
int[][] a = { {1,1,2,2,0,0,0,0},
{3,3,4,4,5,5,0,0},
{-1,-1,-2,-2,-3,-3,-4,-4},
{2,2,0,0,0,0,0,0} };
int[][] result = new int[a.length][];
for( int i=0; i<a.length; i++ )
{
//算出当前第二维数组的长度
int size = 0;
for( int j=0; j<a[i].length; j++ )
{
if(a[i][j]!=0)
size++;
}
//复制非0数据
result[i] = new int[size];
int pos = 0;
for( int j=0; j<a[i].length; j++ )
{
if(a[i][j]!=0)
{
result[i][pos] = a[i][j];
pos++;
}
}
}
//打印结果
for( int i=0; i<result.length; i++ )
{
for( int j=0; j<result[i].length; j++ )
{
System.out.print( result[i][j] + "," );
}
System.out.println();
}
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;public class TestArr {
public static void main(String args[]){
int a[][] ={{1,1,2,2,0,0,0,0},{3,3,4,4,5,5,0,0},{-1,-1,-2,-2,-3,-3,-4,-4},{2,2,0,0,0,0,0,0}};
List<Integer> list = new ArrayList<Integer>();
Map<Integer,List> map = new HashMap<Integer,List>();
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[i].length; j++){
if(a[i][j] == 0)
continue;
else{
list.add(a[i][j]);
}
}
map.put(i, list);
list = new ArrayList<Integer>();
} int b[][] = new int[a.length][];
Iterator it = map.entrySet().iterator();
for(int i = 0; i < a.length; i++){
Map.Entry entry =(Map.Entry)it.next();
b[i] = new int[((List)entry.getValue()).size()];
for(int j = 0; j < ((List)entry.getValue()).size(); j++)
b[i][j] = (Integer)((List)entry.getValue()).get(j);
}
for(int i = 0; i < b.length; i++){
for(int j = 0; j < b[i].length; j++)
System.out.print(" " + b[i][j]);
System.out.println();
}
}
}
for(int i = 0;i<b.length;i++)
{
System.out.println(Arrays.toString(b[i]));
}
打印会发现有空数组啊
public static void main(String[] args) {
int[][] a = { { 1, 1, 2, 2, 0, 0, 0, 0 }, { 3, 3, 4, 4, 5, 5, 0, 0 },
{ -1, -1, -2, -2, -3, -3, -4, -4 }, { 2, 2, 0, 0, 0, 0, 0, 0 } };
List<List> list = new ArrayList<List>();
for (int i = 0; i < a.length; i++) {
List t = new ArrayList();
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] != 0) {
t.add(a[i][j]);
}
}
list.add(t);
} System.out.println(list);
}
{
public static int[][] deleteZero( int[][] a )
{
ArrayList list = new ArrayList();
for( int i=0; i<a.length; i++ )
{
//算出当前第二维数组的长度
int size = 0;
for( int j=0; j<a[i].length; j++ )
{
if(a[i][j]!=0)
size++;
}
//如果全部为0,跳过
if( size==0 )
continue;
//复制非0数据
int[] array = new int[size];
int pos = 0;
for( int j=0; j<a[i].length; j++ )
{
if(a[i][j]!=0)
{
array[pos] = a[i][j];
pos++;
}
}
list.add( array );
}
//生成结果数组
int[][] result = new int[list.size()][];
for( int i=0; i<result.length; i++ )
result[i] = (int[])list.get( i );
return result;
}
public static void main( String[] args )
{
int[][] a = { {1,1,2,2,0,0,0,0},
{3,3,4,4,5,5,0,0},
{-1,-1,-2,-2,-3,-3,-4,-4},
{0,0,0,0,0,0,0,0} }; int[][] result = deleteZero( a );
//打印结果
for( int i=0; i<result.length; i++ )
{
for( int j=0; j<result[i].length; j++ )
{
System.out.print( result[i][j] + "," );
}
System.out.println();
}
}
}