标签:
数组注意点:
1:数组在使用之前长度是固定的2016-04-28
2:数组的索引从0开始
3:定义数组 int[] nums = {1,2,3,4}或 int[] nums = new int[4];nums[0] =1;....
4:下面列题为:在指定长度的数值中插入一个字符串,并重新排序并输出。
1 public class HelloWorld11 {
2
3 public static void main(String[] args) {
4 String[] musics = {"csdf","asdd","bbsf","dssf"};
5 // 冒泡排序(两两比较,大的往下沉,每次求出一个最大,抛开最大再求第二大值)
6 for(int j = 1;j< musics.length-1; j++){
7 for(int i = 0;i< musics.length-j; i++){
8 char shou = musics[i].charAt(0);
9 char shoulater = musics[i+1].charAt(0);
10 if(shou > shoulater){
11 String middle = musics[i];
12 musics[i] = musics[i+1];
13 musics[i+1] = middle;
14 }
15 }
16 }
17 // 输入一个字符串
18 String[] charuhoushuzu = new String[5];
19 System.out.println("请输入英文字符串");
20 Scanner input = new Scanner(System.in);
21 String f = input.next();
22 char fo = f.charAt(0);
23 //外层循环找索引,内层循环在指定索引位置插入元素
24 for(int m = 0; m < musics.length; m++){
25 if(fo < musics[m].charAt(0)){
26 for(int n = 0; n < charuhoushuzu.length;n++){
27 if(n < m){
28 charuhoushuzu[n] = musics[n];
29 }else if(n == m){
30 charuhoushuzu[m] = f;
31 }else{
32 charuhoushuzu[n] = musics[n-1];
33 }
34 }
35 break;
36 }else if(fo > musics[musics.length-1].charAt(0)){
37 charuhoushuzu[m] = musics[m];
38 charuhoushuzu[charuhoushuzu.length - 1] =f;
39 }
40 }
41 // 遍历数组(排序后)
42 for(int k = 0;k< charuhoushuzu.length; k++){
43 System.out.println(charuhoushuzu[k]);
44 }
45
46 }
47
48 }
标签:
原文地址:http://www.cnblogs.com/xieshunjin/p/5444843.html