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]+" ");
}
} |