zoukankan      html  css  js  c++  java
  • 数组的使用

    数组的使用

    数组的使用

    我们定义两个变量int a=5;int b=6; 如果想交换a,b的值,不能将两个变量的值直接赋值给对方,列如

    a = b;
    b = a;
    

    这样最终的结果是两个变量的值都是6.
    正确的做法是定义一个临时变量:

    int tmp = a;
    a = b;
    b = tmp;
    

    在数组中,排序是一个比较常见的算法。
    通过一个简单的范例,来了解排序的基本操作。

    public class ArrayDemo06{  
    public static void main(String args[]){  
        int score[] = {67,89,87,69,90,100,75,90};  
        for(int i=0;i<score.length;i++){  
            for(int j=0;j<score.length;j++){  
                if(score[i]<score[j]){            //交换位置,这里关系到到底是从大到小还是从小到大!  
                    int temp = score[i];            //中间变量  
                    score[i] = score[j];  
                    score[j] = temp;  
                }  
            }  
        }  
        for(int j=0;j<score.length;j++){            //循环输出  
            System.out.print(score[j]+"	");  
        }  
      }  
    }
  • 相关阅读:
    oracle常用命令(比较常见好用)
    vim编辑器详解
    对话框和打印控件
    winform弹出唯一的窗体
    ListView
    菜单栏和布局
    窗体属性和公共控件
    ASPCMS和WPF
    MVC
    正则表达式
  • 原文地址:https://www.cnblogs.com/lyx1996/p/6973934.html
Copyright © 2011-2022 走看看