当前位置:网站首页>Traversal and splicing of strings
Traversal and splicing of strings
2022-07-24 18:36:00 【The code family】
Traversal of string :
demand : Enter a string on the keyboard , Use the program to traverse the string in the console
Ideas :
① Enter a string on the keyboard , use Scanner Realization
② Traversal string , First, you need to be able to get every character in the string
●public char charAt(int index): Returns the char value , The index of the string is also from 0 At the beginning
③ Traversal string , Secondly, we should be able to get the length of the string
●public int length(): Returns the length of this string
● Length of array : Array name .length
● Length of string : String object .length()
④ The general format for traversing strings
for(int i=0; i<s.length(); i++) {
s. charAt(i); // Is the character value at the specified index
}
import java.util.Scanner;
public class _02_String {
public static void main(String[] args) {
// Enter a string on the keyboard , use Scanner Realization
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter a string :");
String line = sc.nextLine();
// Traversal string , First, you need to be able to get every character in the string
for (int i = 0; i < line.length(); i++) {
System.out.println(line.charAt(i));
}
}
}
String splicing
demand : Define a method , hold int The data in the array is spliced into a string according to the specified format , Call the method , And output the results on the console . for example , The array is int[] arr= {1,2,3};, After executing the method, the output is : [1,2, 3]
Ideas :
① Define a int An array of types , Complete the initialization of array elements with static initialization
② Define a method , Used for holding int The data in the array is spliced into a string according to the specified format . return type String, parameter list int[] arr
③ Traversing arrays in methods , Splice as required
④ Calling method , Receive the result with a variable
⑤ Output results
public class _04_String {
public static void main(String[] args) {
// Define a int An array of types , Complete the initialization of array elements with static initialization
int[] arr = {
1, 2, 3};
// Calling method , Receive the result with a variable
String s = arrayToString(arr);
// Output results
System.out.println("s:" + s);
}
// Define a method , Used for holding int The data in the array is spliced into a string according to the specified format
/* Two clear : return type : String Parameters : int[] arr */
public static String arrayToString(int[] arr) {
String s = "";
// Traversing arrays in methods , Splice as required
s = s + "[";
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
s = s + arr[i];
} else {
s = s + arr[i] + ',';
}
}
s = s + "]";
return s;
}
}
边栏推荐
- 5. Reference type and value type as function parameters?
- 今日睡眠质量记录79分
- Ionic4 learning notes 12 - a east project grid completes the list of goods
- 16. What is the difference between target and currenttarget?
- Matlab simulation of drawing circle on sphere
- Missing value processing
- Getting started with MySQL database
- National vocational college skills competition network security competition -- detailed explanation of Apache security configuration
- Ionic4 learning notes 3
- ES6 cycle filter value
猜你喜欢

EasyUI framework dialog repeated loading problem

Mysql——》BufferPool相关信息

【微信小程序开发】自定义tabBar案例(定制消息99+小红心)

MySQL - bufferpool related information

The 5th Digital China Construction summit opened in Fuzhou, Fujian

Go小白实现一个简易的go mock server

【刷题记录】20. 有效的括号

QT - animation frame

How to render millions of 2D objects smoothly with webgpu?

Type-C边充边听PD协议芯片
随机推荐
Sword finger offer 21. adjust the array order so that odd numbers precede even numbers
微信小程序逆向
Get familiar with pytoch and pytoch environment configuration
Array object methods commonly used traversal methods & higher-order functions
CF Lomsat gelral(启发式合并)
We have to understand the four scopes: application, session, request and page
网络安全80端口—-PHP CGI参数注入执行漏洞
树链剖分板子
Number of times a number appears in an ascending array
Mid year inventory | in 2022, PAAS will be upgraded again
Calling startActivity() from outside of an Activity context requires the FLAG_ ACTIVITY_ NEW_ TASK flag
Cryptography knowledge - Introduction to encryption -1
Valentine's Day gift ----- use her photos and our chat records to generate word clouds~
Several sorting methods for while and sort
Template inheritance and import
["code" power is fully open, and "chapter" shows strength] list of contributors to the task challenge in the first quarter of 2022
空间三点画圆代码
CF. Bits And Pieces(子集状压dp + 剪枝)
CF lomsat gelral (heuristic merge)
epoch,batch_ size