码迷,mamicode.com
首页 > 编程语言 > 详细

Day01 数组

时间:2016-04-29 01:29:57      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

数组注意点:

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 }

 

Day01 数组

标签:

原文地址:http://www.cnblogs.com/xieshunjin/p/5444843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!