zoukankan      html  css  js  c++  java
  • String内存分析,for-each,参数传递

    上午总结:

    蓝白书P245

     

    (一)Iterator用法

    import java.util.*;
    public class HashSetTest{
      public static void main(String[] args){
         Set<String> set=new HashSet<String>();
         set.add(“hyy”);
         set.add(“qxy”);
         set.add(“qjy”);
         set.add(“qxy”);
         System.out.println(“set.size()=”+set.size());
         for(Iterator<String> it=set.iterator();it.hasNext();){
         String str=it.next();
         System.out.println(str);
         }
      }
    }
    /*说明:Iterator<String> it=set.iterator()表示返回集合上的迭代器,用来访问该集合中的各个元素。
                it.hasNext();表示是否有元素可以迭代。    String str=it.next();表示返回到迭代的下一个元素。
                可以把for(int i=0;i<10;++i)对着看,概念和形式上差不多。
    */

    (二)有关字符串“==”和equals的用法和区别

    ==表示字符串变量是否引用自同一个字符串对象

    更常用的是用String类中覆盖Object类的equals方法比较两个字符串变量所引用的字符串对象内容是否相同

    /*字符串比较*/
    public class StringCompareTest{
        public static void main(String[] args) {
            String s1="abc中文";
            String s2="abc中文";
            String s3=new String("abc中文");
            System.out.println(s1==s2);//true
            System.out.println(s1.equals(s2));//true
            System.out.println("----------");
            System.out.println(s1==s3);//false
            System.out.println(s1.equals(s3));//true
        }
    }
    /*
    运行结果:
    true
    true
    ----------
    false
    true
    */

    /*

    理由:String类所修饰的变量表示不可修改的,并且存在一个常量池,且如果前面有一个相同的值的话,当再存入一个相同的值的话,则这两个值指向同一个地址,而==表示地址相同即相同,除非用new关键字另外开辟一个内存空间,比如S3。    而equals只要求两个对象的内容相等即可。

    */

     

    (三).String内存分析

    String str=new String(“corejava”);

    /*在java常量池中找到或创建一个内容为“corejava”的字符串对象,然后在堆上再创建一个对象,且字符串常量池中的内容复制到堆中对象。*/

    String str=“corejava”;

    String类所修饰的变量表示不可修改的,并且存在一个常量池,且如果前面有一个相同的值的话,当再存入一个相同的值的话,则这两个值指向同一个地址,即如果在常量池中存在相同的内容,则不会再创建新的相同的内容,且地址不变。

    /*

     

    (四)关于HashSet实现类   参看蓝白书P247

    代码解析:

    if((this.name==null)?(other.name!=null):!this.name.equals(other.name)){

         return false;

    }

    <==>

    if(this.name==null && other.name!=null)

       return false;

    if(this.name!=null && !this.name.equals(other.name))

      return false;

     

    下午总结:

    蓝白书

    P52 for-each用法

     

    (一)for-each循环

    增强型的for循环也叫for-each循环,用这个语法可以遍历数组或集合。

    语法格式:

    for(数据类型 变量名:数组或集合变量名){

    //通过“变量名”就可以访问数组或集合中的每个元素的值

    }

    比如:

    int array={1,2,3,4,5};

    for(int element :array){

    System.out.println(element);

    }

    //说明:访问数组array中的每个元素element,但无法更新数组元素

     

    红黄书
    P8参数传递
    class A1{
        int a;
        int b;
    }
    
    public class TestParam{
        public void exchange(int x,int y){
            System.out.println("交换前:x="+x+",y="+y);
            int temp=x;
                x=y;
                y=temp;
                System.out.println("交换后:x="+x+",y="+y);
        }
        public void exchange(A1 a){
            System.out.println("交换前:a.a="+a.a+",a.b="+a.b);
            int temp=a.a;
                 a.a=a.b;
                 a.b=temp;
            System.out.println("交换后:a.a="+a.a+",a.b="+a.b);
        }
    
        public static void main(String[] args) {
            A1 a=new A1();
            a.a=1;
            a.b=2;
            TestParam tp=new TestParam();
            int x=5;
            int y=10;
            System.out.println("在main()方法中,交换前:x="+x+",y="+y);
            tp.exchange(x,y);
            System.out.println("在main()方法中,交换后:x="+x+",y="+y);
    
            System.out.println("
    
    在main()方法中,交换前:a.a="+a.a+",a.b="+a.b);
            tp.exchange(a);
            System.out.println("在main()方法中,交换后:a.a="+a.a+",a.b="+a.b);
        }
    }
    /*
    运行前:
    在main()方法中,交换前:x=5,y=10
    交换前:x=5,y10
    交换后:x=10,y=5
    在main()方法中,交换后:x=5,y=10
    
    
    在main()方法中,交换前:a.a=1,a.b=2
    交换前:a.a=1,a.b=2
    交换后a.a=2,a.b=1
    在main()方法中,交换后:a.a=2,a.b=1
    */

    说明:exchange(int x, int y)带整型参数,是值传递,在main方法里面前后不变。

             exchange(A a)带有一个引用传递,地址传递,在main方法里交换前后要变,地址改变成怎样就怎样。

    成年人的世界没有那么多的童话,也没有那么多的逆袭。
  • 相关阅读:
    php 采集程序
    15个实用的PHP正则表达式
    jQuery Mobile优秀网站及资源汇总
    连接ORalce数据库
    Windows Mobile如何观看Webcast
    解决wpf中使用WinformsHost显示滚动条的问题
    利用反射调用类及其属性和方法
    一些实用的vs.net2008 快捷键(已验证)
    WW插件研究
    微软今天公布了下一代Visual Studio和.NET Framework开发工具和平台,该平台将被命名的Visual Studio 2010和.Net Framework 4.0.
  • 原文地址:https://www.cnblogs.com/shijinglu2018/p/8452516.html
Copyright © 2011-2022 走看看