当前位置:网站首页>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;
}
}
边栏推荐
- Sword finger offer 21. adjust the array order so that odd numbers precede even numbers
- Ionic4 learning notes 9 -- an east project 01
- Ionic4 learning notes 7 -- UI component 1 (no practice, direct excerpt)
- 理解动态计算图,requires_grad、zero_grad
- Ionic4 learning notes 13 - Classification List of an East Project
- ES6 cycle filter value
- Introduction to nipple music theory and personal perception
- 5. Reference type and value type as function parameters?
- Ionic4 learning notes 12 - a east project grid completes the list of goods
- 第五届数字中国建设峰会在福建福州开幕
猜你喜欢

9. BOM object?

4. Basic type and reference type?

理解动态计算图,requires_grad、zero_grad

模拟实现vector

QT - animation frame

Wechat applet reverse
![[opencv] - thresholding](/img/4e/88c8c8063de7cb10e44e76e77dbb8e.png)
[opencv] - thresholding

Mysql——》BufferPool相关信息

Calling startActivity() from outside of an Activity context requires the FLAG_ ACTIVITY_ NEW_ TASK flag

Cryptography knowledge - Introduction to encryption -1
随机推荐
Namespace: cluster environment sharing and isolation
Template inheritance and import
网络安全80端口—-PHP CGI参数注入执行漏洞
CF Lomsat gelral(启发式合并)
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
Several sorting methods for while and sort
Ionic4 learning notes 4 -- add a tab page
Get familiar with pytoch and pytoch environment configuration
缺失值处理
Windowing function (1) - top three employees of department salary
【TkInter】常用组件(一)
Pytorch的旅程一:线性模型
Escape character in JS?
Missing value processing
初识Pytorch和Pytorch环境配置
投资的新阶段
Pytoch's Journey 2: gradient descent
6. How to add an array in Es5?
04-分布式资源管理系统YARN
We have to understand the four scopes: application, session, request and page