Common conversion between strings and arrays and numbers in java

Posted by Kestrad on Wed, 05 Jan 2022 18:42:46 +0100

Conversion between character array and string

Character array - > string

  • public static String copyValueOf(char[] data): returns the string representing the character sequence in the specified array.
  • public static String copyValueOf(char[] data, int offset, int count): returns the string representing the character sequence in the specified array.
public class Test {
    public static void main(String args[]) {
        char[] Str1 = {'h', 'e', 'l', 'l', 'o', ' ', 'r', 'u', 'n', 'o', 'o', 'b'};
        String Str2 = "";
 
        Str2 = Str2.copyValueOf( Str1 );
        System.out.println("Return result:" + Str2);
 
        Str2 = Str2.copyValueOf( Str1, 2, 6 );
        System.out.println("Return result:" + Str2);
    }
}

String - > character array

  • s.toCharArray()

Conversion between string and number

Convert a number to a string

  • String ss = String.valueOf(n);
String s = "123";

byte b = Byte.parseByte(s);
short t = Short.parseShort(s);
int i = Integer.parseInt(s);
long l = Long.parseLong(s);
Float f = Float.parseFloat(s);
Double d = Double.parseDouble(s);
boolean bo = Boolean.parseBoolean(s);
char c = Character.parseCharacter(s);
  • i=Integer.valueOf(s).intValue();

Summary:

  • Method 1 directly uses static methods, which will not produce redundant objects, but will throw exceptions.
  • Method 2, integer Valueof (s) is equivalent to new Integer(Integer.parseInt(s)). Exceptions will also be thrown, but one more object will be generated.

Convert string to number

Through the wrapper class corresponding to the basic type, you can convert the string to the basic type. Java provides access to all eight basic types
Required wrapper classes: Boolean corresponds to Boolean, byte corresponds to byte, short corresponds to short, int corresponds to Integer, long corresponds to long, char corresponds to Character, float corresponds to float, and double corresponds to double. All eight wrapper classes provide one
parseXxx(String str) static methods are used to convert strings to primitive types. (Note: if the string is not numeric
String, conversion will result in a runtime error.)

Conversion between List and array

Array to List

  • Arrays.asList(strArray): after converting an array into a List, you can't add or delete the List. You can only check and modify it, otherwise throw an exception.
  • Arrays.asList(strArray): you need to add, delete, modify and query the List after converting the array to List. It can be used when the amount of data in the List is small.
  • Collections.addAll(arrayList, strArray): a List with the same length is created according to the length of the array, and then through collections The addall () method converts the elements in the array into binary and then adds them to the List, which is the most efficient method.

List to array

  • arraylist.toArray(T[] arr): the toArray() method converts an Arraylist object into an array. If the parameter T[] arr is passed into the method as a parameter, an array of type T is returned.
import java.util.ArrayList;
import java.util.Comparator;
class Main {
    public static void main(String[] args){

        // Create a dynamic array
        ArrayList<String> sites = new ArrayList<>();
       
        sites.add("Runoob");
        sites.add("Google");
        sites.add("Wiki");
        sites.add("Taobao");
        System.out.println("Site list: " + sites);

        // Create a new array of String type
        // The array length is the same as the ArrayList length
        String[] arr = new String[sites.size()];

        // Converts an ArrayList object to an array
        sites.toArray(arr);

        // Output all array elements
        System.out.print("Array: ");
        for(String item:arr) {
            System.out.print(item+", ");
        }
    }
}

Topics: Java