SoFunction
Updated on 2025-03-04

Methods for creating and converting integer arrays, string arrays, integer numbers and strings in JAVA

1. Creation of strings, character arrays and integer arrays

1. How to create strings

1.1 Create a string by referencing a character array

char a[]={'A','b','c','E'};
String str1=new String(a);
(str1);

1.2 Define first and then assign value

String str2;
str2="this is a book";
(str2);

1.3 Create a string by intercepting part of a character array

char a3[]={'a','b','c','D','6','p'};
String str3=new String(a3,2,4);
(str3);

1.4 Create a string by instantiating a string of characters through the instantiation object method of the class

String str4=new String("this is a book");
(str4);

2. Create an integer array

2.1 Declare and assign value

int[] arr = {1,2,4, …};

2.2 Declare the array name to open up space and assign values

int[] arr;
arr = new int[]{1,2,3, …};

2.3 When declaring an array, specify the number of elements and then assign a value

int[] arr1= new int[3];

There are also some specific creation methods that can be used to refer to the conversion method.

2. Conversion of integer arrays, string arrays, integer numbers and strings

1. Convert string into character array

  String str="123456";
  char[] c = () ;
  (c);

2. Convert strings into integer arrays

  String str="123456";
  int[] a = new int[()];
  for(int i=0;i<();i++) {
      a[i]  = (i)-'0';
    }

3. Convert strings into integer types

1、int i = (str).intValue();
2、double b = (str); Convert string todoubletype
3、int b = (str);   Convert string tointtype

4. Convert character array into string

char[] c = {'a','s','d','4','5',};
String str = new String(c);
(str);

5. Convert character array into integer array

char[] c = { '1', '2', '3', '4', '5', };
int[] a = new int[];
   for (int i = 0; i < 5; i++) {
       a[i] = c[i] - '0';
       (a[i]);
   }

6. Integer type converted into character type

1、String str = (i);  Convert integer to character
2、String s = (i);  This candoubleType conversion to character type
3、String s = "" + i;

if

String str1 = “ad45nfdf”;

but

(0) is for “a”;

(7) is "f";

3. Some methods in strings

1、indexof()

There are four methods for searching substrings in strings in Java (indexof())

The indexOf method returns an integer value indicating the start position of the substring inside the String object. If no substring is found, return -1.
If startindex is a negative number, startindex is treated as zero. If it is larger than the largest character position index, it is regarded as the largest possible index.

There are four methods for searching substrings in strings in Java, as follows:
① int indexOf(String str): Returns the index of the specified substring that appears for the first time in this string.
② int indexOf(String str, int startIndex): Starting from the specified index, return the index of the specified substring that appears for the first time in this string.
③ int lastIndexOf(String str): Returns the index of the specified substring that appears to the rightmost in this string.
④ int lastIndexOf(String str, int startIndex): Starts searching backward from the specified index, and returns the index of the specified substring that last appeared in this string.

Applied to algorithm problems.

Topic: Find the first character that only appears once in a string (0<= string length <=10000, all composed of letters), and return its position, if not, return -1 (need to be case sensitive). (Counting starts from 0)

import .*;
public class Solution {
    public int FirstNotRepeatingChar(String str) {
        for(int i = 0;i<();i++){
            if(((i))==i && ((i),i+1)==-1) return i;
        }
        return -1;
       }
}

Analysis: If the input parameter is "google"

Condition 1: ((i))==i  The function is to use the first method ① to determine that the character appears for the first time.

Condition 2: ((i),i+1)==-1 is to find the same character as i position from the i+1 position. If it is -1, it means it does not exist.

If there is no condition one, the return value is 2, because for the second o, there is no o after it.

If there is no condition two, the return value is 0 because for the first g it is the first g (i.e. there is no other g before it)

This is the article about the creation and conversion of integer arrays, string arrays, integer numbers and strings in JAVA. For more related Java integer arrays, string arrays, integer numbers and string content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!