標題:
陣列元素移除範例
[打印本頁]
作者:
ray
時間:
2012-3-24 20:03
標題:
陣列元素移除範例
public class ArrayRemove {
public static int[] Remove(int[] a , int i)
{
int[] b = a;
if(i < a.length)
{
b = new int[a.length-1];
int j = 0,k = 0;
for(j=0;j<a.length;j++)
{
if(j != i)
{
b[k] = a[j];
k++;
}
}
}
return b;
}
public static String[] Remove(String[] a , int i)
{
String[] b = a;
if(i < a.length)
{
b = new String[a.length-1];
int j = 0,k = 0;
for(j=0;j<a.length;j++)
{
if(j != i)
{
b[k] = a[j];
k++;
}
}
}
return b;
}
public static void main(String[] args)
{
System.out.println("數字陣列測試");
int[] n = {1,2,3,4,5};
for(int i = 0;i<n.length ; i++)
System.out.print(n[i]+" ");
System.out.println();
n = Remove(n,1);
for(int i = 0;i< n.length ; i++)
System.out.print(n[i]+" ");
System.out.println();
System.out.println("字串陣列測試");
String[] s = {"aaa","bbb","ccc","ddd","eee"};
for(int i = 0;i<s.length ; i++)
System.out.print(s[i]+" ");
System.out.println();
s = Remove(s,1);
for(int i = 0;i< s.length ; i++)
System.out.print(s[i]+" ");
}
}
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2