zoukankan      html  css  js  c++  java
  • JAVA进阶6

    间歇性混吃等死,持续性踌躇满志系列-------------第6天

    1、数组

     1 package cn.intcast.day06.demo01;
     2 
     3 /*
     4 直接打印数组名称,得到的是数组对应的内存地址的哈希值.
     5 访问数组元素的格式:数组名称[索引值]
     6 */
     7 public class array01 {
     8     public static void main(String[] args) {
     9         //静态初始化的省略格式
    10         int[] arr = {10, 20, 30, 40};
    11         int[] arr1 = new int[4];
    12         System.out.println(arr);   // [I@16d3586
    13         System.out.println(arr1);   // [I@154617c
    14         System.out.println(arr[0]);
    15         System.out.println(arr1[0]);
    16         System.out.println(arr1[1]);
    17         System.out.println(arr1[2]);
    18         System.out.println(arr1[3]);
    19     }
    20 }
    View Code

    运行结果图

    2、JAVA的内存划分

    java的内存划分为5个部分:

    ①栈(stack):存放的都是方法中的局部变量。方法的运行一定要在栈当中运行。

    局部变量:方法的参数,或者是方法{}内部的变量

    作用域:一旦超出作用域,立刻从栈内存当中消失。

    ②堆(heap):凡是new出来的东西,都在堆中。

    堆内存中的东西都有一个地址值:16进制

    堆内存里面的数据都有默认值。规则:

        如果是整数          默认为0

        如果是浮点数       默认为0.0

        如果是字符           默认为‘u0000'

        如果是布尔           默认为false

        如果是引用类型    默认为null

    ③方法区(method area):存储.class相关信息,包含方法的信息

    ④本地方法栈(native method stack):与操作系统相关

    ⑤寄存器(pc register):与cpu相关

    3、数组的遍历输出

     1 package cn.intcast.day06.demo01;
     2 
     3 public class array01 {
     4     public static void main(String[] args) {
     5         int arr[] = {2,22,33,44,44,4,4,43,2,3,2,};
     6         for (int i = 0; i < arr.length; i++) {
     7             System.out.print(arr[i] + ",");
     8         }
     9     }
    10 }
    View Code

    运行结果图

    4、数组求最大值

     1 package cn.intcast.day06.demo01;
     2 
     3 public class array01 {
     4     public static void main(String[] args) {
     5         //定义数组
     6         int arr[] = {2, 22, 33, 444, 44, 43, 3, 1000};
     7         //定义最大值存放的参数
     8         int max = arr[0];
     9         //循环比较
    10         for (int i = 0; i < arr.length; i++) {
    11             if (arr[i] > max) {
    12                 max = arr[i];
    13             }
    14         }
    15         System.out.println("最大值是:" + max);
    16     }
    17 }
    View Code

    运行结果图

     

    5、数组的反转

     1 package cn.intcast.day06.demo01;
     2 
     3 public class array01 {
     4     public static void main(String[] args) {
     5         //定义数组
     6         int arr[] = {11,22,33,44,55,66,77,88,99};
     7         for (int i = 0; i < arr.length; i++) {
     8             System.out.print(arr[i]+",");
     9         }
    10         System.out.println();
    11         for (int min =0,max=arr.length-1;min<max;min++,max--) {
    12             int temp = 0;
    13             temp = arr[min];
    14             arr[min] = arr[max];
    15             arr[max] = temp;
    16         }
    17         for (int i = 0; i < arr.length; i++) {
    18             System.out.print(arr[i]+",");
    19         }
    20     }
    21 }
    View Code

    运行结果图

    6、数组作为方法返回值

     1 package cn.intcast.day06.demo01;
     2 
     3 public class array01 {
     4     public static void main(String[] args) {
     5         int[] re = caculate(1, 3, 4);
     6         System.out.println(re[0]);
     7         System.out.println(re[1]);
     8     }
     9 
    10     public static int[] caculate(int a, int b, int c) {
    11         int sum = a + b + c;
    12         int avg = sum / 3;
    13         int[] array = new int[2];
    14         array[0] = sum;
    15         array[1] = avg;
    16         return array;
    17     }
    18 }
    View Code

    运行结果图

     

    7、手机类练习

     1 package cn.intcast.day06.demo01;
     2 /*
     3 *定义一个类,用来模拟“手机”事物
     4 * 属性:品牌、价格、颜色
     5 * 行为:打电话、发短信
     6 *
     7 * 对应到类当中
     8 * 成员变量(属性):
     9 *       String brand;  //评判
    10 *       double price;  //价格
    11 *       String color;  //颜色
    12 * 成员方法(行为):
    13 *       public void call(String who){}  //打电话
    14 *       public void sendMessage(){}     //群发短信
    15 * */
    16 public class phone {
    17     String brand;
    18     double price;
    19     String color;
    20     public void call(String who){
    21         System.out.println("给"+who+"打电话");
    22     }
    23     public void sendMessage(){
    24         System.out.println("发短信");
    25     }
    26 }
    phone.java
     1 package cn.intcast.day06.demo01;
     2 
     3 public class phoneone {
     4     public static void main(String[] args) {
     5         //根据phone类,创建一个名为one的对象
     6         //格式:类名称 对象名 = new 类名称();
     7         phone one = new phone();
     8         System.out.println(one.brand);
     9         System.out.println(one.color);
    10         System.out.println(one.price);
    11         System.out.println("======================");
    12         one.color = "绿色";
    13         one.brand = "华为";
    14         one.price = 9999;
    15         System.out.println(one.brand);
    16         System.out.println(one.color);
    17         System.out.println(one.price);
    18         one.call("Jane");
    19         one.sendMessage();
    20     }
    21 }
    phoneone

    运行结果图

    对象的内存图

     

  • 相关阅读:
    工作计划
    bzoj3626:[LNOI2014]LCA
    bzoj3631:[JLOI2014]松鼠的新家
    bzoj3573: [Hnoi2014]米特运输
    bzoj4027,[HEOI2015]兔子与樱花
    bzoj3624,[Apio2008]免费道路
    bzoj2208连通数
    tyvj1153/洛谷P1262间谍网络
    Application server libraries not found && IntelliJ IDEA && tomcat
    debian9安装java8
  • 原文地址:https://www.cnblogs.com/Anemia-BOY/p/10549696.html
Copyright © 2011-2022 走看看