• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Picking up where we left off: Today’s lesson is arrays and strings in Java

An array of

  • Array declaration: An array is an ordered collection of elements of the same data type. Arrays are used to store data of the same type in contiguous memory locations. Elements in an array are of the same type, and are accessed by subscripts starting at 0.

    Type array name [];

    A type is the type of an array element. The array name is a valid identifier. [] specifies that an array type variable is defined.

  • Create an array

    The array declaration simply defines an array reference. The system does not allocate any memory for the array, so it cannot access any of its elements yet. The elements of an array must be data initialized before they can be used. The initialization process is the creation of the array.

    Arrays can be initialized statically and dynamically. Static initialization refers to assigning initial values to array elements while defining them. Static initialization uses a pair of braces to enclose the initial value.

    Int intArray[] = {1, 2, 3, 4, 5}; // The dynamic initialization example uses the operator new to allocate space to an array, as with all objects. The number in square brackets in an array declaration represents the number of elements in the array. String names[]; names = new String[4]; names[0] = "Georgianna"; names[1] = "Jen"; names[2] = "Simon"; names[3] = "Tom";Copy the code
  • Accessing an array element

    In Java, array subscripts start at 0, and length is the only data member variable in the array class. The system automatically assigns a value to length when creating an array using new. Once an array is created, its size is fixed. Program run time can use length for array boundary checking.

    Class Calculator {public static double calculateAverage (int [] numbers) {int sum = 0; for (int i = 0; i < numbers.length; i++) { sum += numbers[i]; } return sum /(double)numbers.length } public static int findMaximun(int[] numbers) { int max = numbers[0]; for (int i = 0; i < numbers.length; i++) { if (number[i] > max) { max = numbers[i] } } return max } }Copy the code
  • Multidimensional array

    • Definition of multidimensional arrays: Array elements can be declared as any type, such as a one-dimensional array type, so that arrays of arrays can be built, that is, two-dimensional arrays.

      Define format: type array name [] []; Type [] [] Array name; Type [] Array name [];

      Like a one-dimensional array, a two-dimensional array is defined with no memory allocated to its elements and is initialized before each element can be accessed.

    • Initialization of a multidimensional array

      Like one-dimensional arrays, there are static and dynamic initializations for multidimensional arrays. Static initialization assigns initial values to array elements while defining the array.

      Int intArray[][] = {{2, 3}, {4, 5}, {6,7}}Copy the code

      When dynamically initializing a two-dimensional array, there are two ways to allocate memory: directly and dimensionally.

      Direct allocation is the direct allocation of space for each dimension, when declaring the array, give the size of each dimension.

      // Type array name [][] = new type [array size][array size]; int a[][] = new int[2][3]Copy the code

      Allocation by dimension starts from the highest dimension (and must start from the highest dimension), and allocates memory for each bit separately. The general format for creating a two-dimensional array is as follows:

      // Type array name [][] = new type [first dimension size][]; int twoDim[][] = new int [4][];Copy the code
    • A reference to a multidimensional array

      Array name [first subscript][second subscript]

  • Common methods of arrays

    • Class used by Array in java.utils
    • Array.fill()Fill the array
    • Arrays.sort()Sort an array
    • Arrays.copyOf()Copy the array
    • Common classes for ArrayUtils in the org.apache.commons package
    • add(final T[] array, final T element)This method adds an element to the specified array;
    • remove(final T[] array, final int index): Removes the element at the index position specified by array red;
    • addAll(final T[] array1, final T... array2): merges two arrays;
    • removeAll(final char[] array, final int... indices): Removes elements at multiple index positions specified by array red;
    • removeElement(final char[] array, final char element): Removes the specified element from the array for the first time;
    • removeAllOccurences(final char[] array, final char element): Removes the specified element from the array.
    • removeElements(final char[] array, final char... values): Removes a specified number of elements from an array and returns a new array.
    • getLength(final Object array): gets the length of the array;
    • contains(final Object[] array, final Object objectToFind): Determines whether the array contains an element.
    • indexOf(final Object[] array, final Object objectToFind): Checks whether an element exists in the array and returns the index position.
    • lastIndexOf(final Object[] array, final Object objectToFind): Finds the specified element starting at the tail;
    • insert(final int index, final T[] array, final T... values): adds an element to the array at the specified index position.

string

A string is a sequence of finite characters, and in Java a string is an object, not an array of characters ending in ‘\0’. Java’s standard package, java.lang, encapsulates the String and StringBuffer classes, making it easy to handle strings. The String class is used to handle immutable strings and the StringBuffer class is used to handle mutable strings.

  • String declaration

    A string is a sequence of zero or more characters in memory. An immutable string means that once a string is created, its contents cannot be changed.

    Strings in Java programs are classified into constants and variables. A string constant is a string of characters enclosed in double quotation marks. The system automatically creates a String for any String constant that appears in the program

    System.out.printIn("This is a String"); // The "This is a String" object will be created, the creation process is implicit.Copy the code
  • String manipulation

    Once a string is created, you can manipulate it using methods in the string class.

    An object instance of the String class is immutable and is determined once created. Performing an operation on a string does not change the string itself, but generates another instance.

    The StringBuffer class handles mutable strings, and when you modify a StringBuffer string, instead of creating a new string object, you manipulate the original string directly.

    The common methods used by both String and StringBuffer classes are as follows:

    • Length () : Returns the length of the string, that is, the number of characters.
    • CharAt (int index): Returns the character at the index position in the string.
    • SubString (int beginIndex): intercepts the subString from beginIndex to the end of the current string.

    Common methods in the String class are as follows

    • Replace (char oldChar, char new char): Converts all oldChar occurrences in the current string to newChar.
    • ToLowerCase (): converts all characters in the current string toLowerCase.
    • ToUpperCase (): converts all characters in the current string toUpperCase.
    • StartsWith (String prefix): Tests whether prefix is the prefix of the current String.
    • Concat (String STR): concatenates STR at the end of the current String.
    • Trim (): Remove Spaces before and after strings.

    The common methods in the StringBuffer class are as follows:

    • Append (String STR): Adds the String represented by the argument STR to the end of the current String.
    • Capacity (): Returns the current capacity.

When the system allocates memory for String objects, the memory is allocated according to the actual number of characters contained in the object. When we allocate memory for a StringBuffer, we add a buffer of 16 characters in addition to the space taken up by the characters. For StringBuffer objects, the length() method takes the length of the string, and the capacity() method returns the current capacity, which is the string length plus the buffer size.

This content ends here, have the wrong place hope everybody corrects in time.