1.字符的排序 2 种方法
- public class Sort
- {
- public static void main(String[] args)
- {
- int[] num1 = {4,3,2,-2,-8,55,33,24,56,23,65,88,457,-23,-66};
- sort(num1);
- }
- public static void sort(int[] num)
- {
- int j=0;
- while(j<num.length)
- {
- for(int i=0;i<num.length-1;i++)
- {
- if(num[i]>num[i+1])
- {
- int temp;
- temp = num[i];
- num[i]=num[i+1];
- num[i+1]=temp;
- }
- }
- j++;
- }
- for(int i=0;i<num.length;i++)
- {
- System.out.print(num[i]+" ");
- }
- }
- }
方法2
// 调用util的Arrays接口
- import java.util.Arrays;
- public class Sort2
- {
- public static void main(String[] args)
- {
- int[] num = {4,3,2,-2,-8,55,33,24,56,23,65,88,457,-23,-66};
- Arrays.sort(num);
- for(int i : num)
- {
- System.out.print(i+ " ");
- }
- System.out.println();
- String[] str = {"Mike","Jerry","Tom","Lucy","Lily","Auther","Yoyo"};
- Arrays.sort(str);
- for(String s : str)
- {
- System.out.print(s + " ");
- }
- }
- }
- 数组的合并
-
三种字符数组合并的方法
public static String[] getOneArray() {
String[] a = { "0", "1", "2" };
String[] b = { "0", "1", "2" };String[] c = new String[a.length + b.length];
for (int j = 0; j < a.length; ++j) {
c[j] = a[j];
}for (int j = 0; j < b.length; ++j) {
c[a.length + j] = b[j];
}return c;
}//直接新建数组重新赋值public static Object[] getTwoArray() {
String[] a = { "0", "1", "2" };
String[] b = { "0", "1", "2" };List aL = Arrays.asList(a);
List bL = Arrays.asList(b);List resultList = new ArrayList();
resultList.addAll(aL);
resultList.addAll(bL);Object[] result = resultList.toArray();
return result;
}//调用ArrayLIst中转,添加数组到Listpublic static String[] getThreeArray() {
String[] a = { "0", "1", "2", "3" };
String[] b = { "4", "5", "6", "7", "8" };
String[] c = new String[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}//System.arraycopy 方法将数组放到新建的组合数组中1.两个字符数组合并的问题
public String[] getMergeArray(String[] al,String[] bl) {
String[] a = al;
String[] b = bl;
String[] c = new String[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}2.字符数组和整形数组合并问题
public int[] getIntArray(int[] al,String[] bl) {
int[] a = al;
String[] b = bl;
int[] ia=new int[b.length];
for(int i=0;i<b.length;i++){
ia[i]=Integer.parseInt(b[i]);
}int[] c = new int[a.length + ia.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(ia, 0, c, a.length, ia.length);
return c;
}//数字字符与数字的组合 -