Java Road --- Day07

2019-10-21-23:30:24


ArrayList class [set]

  What: of java.util.ArrayList achieve variable size of the array, including the data storage element is called an element, such to provide a method of operating an internal storage element

  Why: Variable ArrayList size, so AttayList can continue to add elements, its size also automatically increase.

    1. The length of the array can not be changed, but the length is set ArrayList changes that may occur

  How: the ArrayList <E> object name = new ArrayList <E> () ;

    <E> is called generic, represents one of the specified data type, reference data may be used (not to replace the basic type) Type replace, E the right side can be omitted

 

  Precautions:

    1.ArrayList collection, the direct printing get is the content, not the address value, and an array of print address value is obtained

    2. If the content is empty, the print is obtained in brackets, []

  Common method

    1.public boolean add (E e); adding them to the collection element, and the same generic type parameter. The return value represents the add is successful.

    Note: For Arraylist collection is, add to add some action is successful, the returned value may or may not. But for other collections (future learning) for, add add actions are not necessarily successful.

 

    2.public E remove (int index): In addition to elements from among a set of books, the parameter is the index number, the return value is to be removed elements

    3. public GET E (int index), among the elements obtained from the set parameter is the index number, the return value is the element corresponding to the position.

    4. public int size (): Get the length dimension set, the return value is the number of elements contained in the collection.

package demoarraylist;

import java.util.ArrayList;

public  class ArrayListPra01 {
     public  static  void main (String [] args) {
         // create a collection object ArrayList 
        ArrayList <String> = the arrayList new new ArrayList <> ();
         // prints empty set 
        System.out.println (arrayList); // []
         // add elements to the collection 
        Boolean success = arrayList.add ( "Galen" );
        System.out.println (arrayList); // [Galen] 
        System.out.println ( "Add success of the action:" + Success); // whether to add the action was successful: to true 
        arrayList.add ( "Prince" ) ;
        arrayList.add ( "Debon" );
        arrayList.add ( "Juggernaut" );
        System.out.println (arrayList); // print added element

        // Get element from the collection 
        System.out.println (arrayList.get (0)); // Galen 
        System.out.println (arrayList.get (. 1)); // Prince 
        System.out.println (arrayList. GET (2)); // Nadu 
        System.out.println (arrayList.get (. 3)); // blademaster

        // delete elements of the collection 
        String name = arrayList.remove (3 );
        System.out.println ( "is deleted:" + name); // be deleted are: Juggernaut 
        System.out.println (arrayList); // [Galen, prince, Debon]

        // get the length set, also known as the number of elements 
        int size = arrayList.size ();
        System.out.println ( "set length is:" + size); // length set is: 3 
    }
}

 

String:

  What: . Java.lang.String class represents all strings in the program double-quoted strings, are objects of the String Class

  String features:  

    Contents 1. Never string variable. [Focus]

    2. It is precisely because the string can not be changed, so the strings can be shared use.

    3 corresponds to the effect of the string is char [] array of characters, but the underlying principle is the byte [] array of bytes.

  3 + 1 to create common ways strings.

  Method three configurations:

    public strin G (): Create a blank string does not contain any content.

    public String (char [] array) : The contents of the character array to create the corresponding character string.

    public String (byte [] Array): The contents of the byte array to create the corresponding character string.

  A direct creation: String str = "the Hello"; // use double quotation marks directly on the right

  Note: Direct write double quotes is the string object.

 

package demostring;

public  class StringPra01 {
     public  static  void main (String [] args) {
         // empty argument constructor 
        String str1 = new new String (); // parentheses empty, what strings are not described. 
        . System out println ( "1st string:" +. Str1);
         // create a string from the character array 
        char [] = {charArray 'A', 'B', 'C' };
        String str2 = new String( charArray);
        . The System OUT the println (. "Second string:" + str2); // 2nd string: the ABC
         // create a string according to the byte array 
        byte [] = {byteArray The 97, 98, 99 };
        String str3 = new String(byteArray);
        System.out.println ( "3rd string:" + str3); // 3rd string: abc
         // directly create 
        String str4 = "the Hello" ;
        System.out.println ( "Fourth string:" + str4); // fourth string: the Hello 
    }
}

 

String constant pool:

String comparison method:

  == address comparison object value, if the content does require a string comparison, two methods may be used

  Two methods:
    public booleon equols (Object obj), the parameter can be any object, only parameter is a string and the same content will give true, otherwise false.

    public boolean equalsIgnoreCase (String str), ignoring case, the content comparison.
  Note,
    1. Any person can take reception 0bject.
    2. equals method of symmetry, i.e. a. Equals (b), and b. Equals (a) the same effect.
    3. If the two sides compare a constant to a variable, the constant string EDITORIAL recommended.
    "Abc" .equats (str), not recommended str.equals ( "abc"): the recommended
method for obtaining the string

 

  String common method which has related to access:
    public int length (), which acquires the number of characters contained in the string, the string length to get.
    public String concat (String str): The current string parameters and return values string concatenation become a new string.
    public char charAt (int index), gets the specified index position of a single character. (From zero prime primer)
    public int indexOf (String STR): find the index position of the first occurrence of the parameter string of this character string in which, if there is no return value of -1.

String interception method:

  public string substring(int index); 截取从参数位置一直到字符串末尾,返回新字符串。
  public String substring(int begin, int end); 截取从begin开始,一直到end结束,中间的字符串。
  备注。[begin,end), 包含左边,不包含右边。

字符串的转换:

 

  public char[] toCharArray(); 将当前字符串拆分成为字符数组作为返回值。
  public byte[] getBytes():获得当前字符串底层的字节数组。
  public String replace(CharSequence oldString, CharSequence newString);
  将所有出现的老字符串替换成为新的字符串,返回替换之后的结果新字符串。
  备注: CharSequence意思就是 说可以接受字符串类型。

字符串的分隔:

  public String[] split(String regex): 按照参数的规则,将字符串切分成为若干部分。
  注意事项:
  split方法的参数其实是一个 “正则表达式”,今后学习。
  今天要注意:如果按照英文句点“。进行切分。必须写"\\." (两个反斜杠)


有点晚了,后面的代码明天再补回去

 

Guess you like

Origin www.cnblogs.com/hpcz190911/p/11717416.html