java split() method return type question

Asked 1 years ago, Updated 1 years ago, 63 views

Hello, I'm trying to use split() method in Java.

        Scanner reader = new Scanner(System.in);
        String inputValue = reader.nextLine();
        System.out.println(inputValue); //5,6,7
        String[] splitedValue = inputValue.split(",");
        System.out.println(splitedValue); //[Ljava.lang.String;@b97c004 
        System.out.println(splitedValue[0]); // 5

After receiving the string, you tried to put the value based on the comma. In the case of Python, if you print the tempList, the separated list is printed according to the comma.

temp = "4,5,6"
tempList = temp.split(",")
print(tempList) // ['4', '5', '6']

I tried to print it out to see what was in the split value to do the same process in java Outputs Ljava.lang.String;@b97c004. After a little bit of googling, I think that means splitValue is a string array type. https://coderanch.com/t/327283/java/class-Ljava-lang-String

Question 1) I printed out a variable, but why did you print out the type of variable?

Question 3) If you output an array from the code below, the number after @ is called the address. Why do you print these values?

        int[] intArr = {5,6}; 
        String[] strArr = {"a","b"}; 
        System.out.println(intArr);  // [I@506e6d5e
        System.out.println(strArr); //[Ljava.lang.String;@2acf57e3

Question 2) What should I do to output ['4', '5', '6'] like Python's method? Self-answer

import java.util.Arrays;
System.out.println(Arrays.toString( splitedValue));

You can use the toString() method as shown in .

Thank you for reading my question! :)

java split

2022-09-22 18:22

1 Answers

Question1)

The array in Java is designed to use the toString() method of the Object class, called the top-level class.
Typically, when you create a class, you override the toString() method.

Object.class

public String toString()
{
      return getClass().getName()+"@"+Integer.toHexString(hashCode());
}

L: One-dimensional array
Type: java.lang.String;
separator: @
Hash value in hex: b97c004

Question 2)

There are many ways Most basically, the repeat statement for , for each and There is a way to use the toString() in the Arrays class If you use Java 8 or higher, there are many more ways.


2022-09-22 18:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.