Summer Java study notes (3)

1. Parameter passing: Java parameters are passed into the method in the form of value passing, not reference passing. When passing a parameter to a method, it is essentially passing the address of the object into the formal parameter by value.

        You can see that in the following code, if you setName before new Dog("B"), the name of the original dog will be changed at this time, because the incoming address has not been changed;

        But if a new dog address is modified after new, the name of the original dog will not be changed at this time, so it is passed in by value

//PassByValueExample类
public class PassByValueExample {
    public static void main(String[] args) {
        Dog dog = new Dog("A");
        System.out.println(dog.getObjectAddress());//basic.参数传递.Dog@1540e19d
        func(dog);
        System.out.println(dog.getObjectAddress());//basic.参数传递.Dog@1540e19d
        System.out.println(dog.getName());//A  C
    }

    private static void func(Dog dog){
        System.out.println(dog.getObjectAddress());//basic.参数传递.Dog@1540e19d
        dog.setName("C");//如果不new一个dog,这是原地址的dog对象名会被改变
        dog = new Dog("B");//此时定义了新的dog对象,后续地址会改变
        System.out.println(dog.getObjectAddress());//basic.参数传递.Dog@677327b6
        System.out.println(dog.getName());
        dog.setName("D");
        System.out.println(dog.getObjectAddress());//basic.参数传递.Dog@677327b6
        System.out.println(dog.getName()); //D
    }
}

//Dog类
public class Dog {
    String name;

    Dog(String name){
        this.name = name;
    }

    String getName(){
        return this.name;
    }

    void setName(String name){
        this.name = name;
    }

    String getObjectAddress(){
        return super.toString();
    }
}

2.List——ArrayList和LinkedList

Using ArrayList and LinkedList requires import related packages

ArrayList - no fixed size limit, we can add or remove elements

        It is suitable for frequently accessing an element in the list, or only needs to add and delete elements at the end of the list.

LinkedList——Linked list is a common basic data structure. It is a linear table, but it does not store data in a linear order, but stores the address of the next node in each node.

        It is suitable for operations where you need to access some elements in the list through loop iteration, or need to frequently add and delete elements at the beginning, middle, and end of the list.

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;

public class List类 {
    public static void main(String[] args) {
//        ArrayListMeth();
        LinkedListMeth();
    }

    static void ArrayListMeth(){
//        ArrayList<E> objectName =new ArrayList<>();  // 初始化 E为数据类型
        ArrayList<String> sites = new ArrayList<>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("TaoBao");
        sites.add("Weibo");
//        System.out.println(sites);
//        System.out.println(sites.get(0)); //获取元素
        for(String s:sites) System.out.println(s);
        ArrayList<Integer> num = new ArrayList<>();
        num.add(10);
        num.add(40);
        num.add(30);
        num.add(20);
        Collections.sort(num);
        for(int i:num) System.out.println(i);


//        boolean	Boolean
//        byte	Byte
//        short	Short
//        int	Integer
//        long	Long
//        float	Float
//        double	Double
//        char	Character

//        add()	    将元素插入到指定位置的 arraylist 中
//        addAll()	添加集合中的所有元素到 arraylist 中
//        clear()	删除 arraylist 中的所有元素
//        clone()	复制一份 arraylist
//        contains()	判断元素是否在 arraylist
//        get()	通过索引值获取 arraylist 中的元素
//        indexOf()	返回 arraylist 中元素的索引值
//        removeAll()	删除存在于指定集合中的 arraylist 里的所有元素
//        remove()	删除 arraylist 里的单个元素
//        size()	返回 arraylist 里元素数量
//        isEmpty()	判断 arraylist 是否为空
//        subList()	截取部分 arraylist 的元素
//        set()	替换 arraylist 中指定索引的元素
//        sort()	对 arraylist 元素进行排序
//        toArray()	将 arraylist 转换为数组
//        toString()	将 arraylist 转换为字符串
//        ensureCapacity()	设置指定容量大小的 arraylist
//        lastIndexOf()	返回指定元素在 arraylist 中最后一次出现的位置
//        retainAll()	保留 arraylist 中在指定集合中也存在的那些元素
//        containsAll()	查看 arraylist 是否包含指定集合中的所有元素
//        trimToSize()	将 arraylist 中的容量调整为数组中的元素个数
//        removeRange()	删除 arraylist 中指定索引之间存在的元素
//        replaceAll()	将给定的操作内容替换掉数组中每一个元素
//        removeIf()	删除所有满足特定条件的 arraylist 元素
//        forEach()	遍历 arraylist 中每一个元素并执行特定操作

//        以下情况使用 ArrayList :
//          频繁访问列表中的某一个元素。
//          只需要在列表末尾进行添加和删除元素操作。
    }

    static void LinkedListMeth(){
        LinkedList<String> sites = new LinkedList<>();
        sites.add("Weibo");
        sites.add("Runoob");
        sites.addFirst("Wiki");
        System.out.println(sites);
        System.out.println(sites.poll());

//        以下情况使用 LinkedList :
//          你需要通过循环迭代来访问列表中的某些元素。
//          需要频繁的在列表开头、中间、末尾等位置进行添加和删除元素操作。
//        public boolean add(E e)	链表末尾添加元素,返回是否成功,成功为 true,失败为 false。
//        public void add(int index, E element)	向指定位置插入元素。
//        public boolean addAll(Collection c)	将一个集合的所有元素添加到链表后面,返回是否成功,成功为 true,失败为 false。
//        public boolean addAll(int index, Collection c)	将一个集合的所有元素添加到链表的指定位置后面,返回是否成功,成功为 true,失败为 false。
//        public void addFirst(E e)	元素添加到头部。
//        public void addLast(E e)	元素添加到尾部。
//        public boolean offer(E e)	向链表末尾添加元素,返回是否成功,成功为 true,失败为 false。
//        public boolean offerFirst(E e)	头部插入元素,返回是否成功,成功为 true,失败为 false。
//        public boolean offerLast(E e)	尾部插入元素,返回是否成功,成功为 true,失败为 false。
//        public void clear()	清空链表。
//        public E removeFirst()	删除并返回第一个元素。
//        public E removeLast()	删除并返回最后一个元素。
//        public boolean remove(Object o)	删除某一元素,返回是否成功,成功为 true,失败为 false。
//        public E remove(int index)	删除指定位置的元素。
//        public E poll()	删除并返回第一个元素。
//        public E remove()	删除并返回第一个元素。
//        public boolean contains(Object o)	判断是否含有某一元素。
//        public E get(int index)	返回指定位置的元素。
//        public E getFirst()	返回第一个元素。
//        public E getLast()	返回最后一个元素。
//        public int indexOf(Object o)	查找指定元素从前往后第一次出现的索引。
//        public int lastIndexOf(Object o)	查找指定元素最后一次出现的索引。
//        public E peek()	返回第一个元素。
//        public E element()	返回第一个元素。
//        public E peekFirst()	返回头部元素。
//        public E peekLast()	返回尾部元素。
//        public E set(int index, E element)	设置指定位置的元素。
//        public Object clone()	克隆该列表。
//        public Iterator descendingIterator()	返回倒序迭代器。
//        public int size()	返回链表元素个数。
//        public ListIterator listIterator(int index)	返回从指定位置开始到末尾的迭代器。
//        public Object[] toArray()	返回一个由链表元素组成的数组。
//        public T[] toArray(T[] a)	返回一个由链表元素转换类型而成的数组。
    }

}

3. Hash related - HashSet and HashMap

Using HashSet and HashMap also requires import related packages.

HashSet - is a collection that does not allow duplicate elements

HashMap - is a hash table that stores key-value pairs (key-value) mapping



import java.util.HashMap;
import java.util.HashSet;

public class 哈希类 {
    public static void main(String[] args) {
//        HashSetMeth();
        HashMapMeth();
    }

    static void HashSetMeth(){
        HashSet<String> sites = new HashSet<>();
        sites.add("Weibo");
        sites.add("RedBook");
//        sites.add("Weibo"); //不会重复添加相同的元素
        System.out.println(sites);
    }

    static void HashMapMeth(){
//        HashMap 是一个散列表,它存储的内容是键值对(key-value)映射
        HashMap<Integer, String> sites = new HashMap<>();
        sites.put(1,"Weibo");
        sites.put(2,"RedBook");
        sites.put(3,"Runoob");
//        System.out.println(sites);
//        System.out.println(sites.get(1));
        for(Integer i:sites.keySet()) System.out.println("key:"+i+"value:"+sites.get(i));
        for(String value:sites.values()) System.out.print(value+",");

//        boolean	Boolean
//        byte	Byte
//        short	Short
//        int	Integer
//        long	Long
//        float	Float
//        double	Double
//        char	Character
    }
}

Guess you like

Origin blog.csdn.net/laodaye_xiaolizi/article/details/130997186