Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method steps of inserting a character in the Java array

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the method and steps of inserting a character in the Java array". In the daily operation, I believe that many people have doubts about the methods and steps of inserting a character in the Java array. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "the method and steps of inserting a character in the Java array". Next, please follow the editor to study!

Catalogue

Define an array

Define inserted characters

Order of characters before print insertion

Assume the insertion position

Find the insertion location

Array data move down

Move in value

Output array

Define an array public class charInsert {public static void main (String [] args) {/ / this is an array of characters char [] ch = new char [9]; ch [0] = 'asides; ch [1] =' baked; ch [2] = 'cations; ch [3] =' fags; ch [4] = 'gathers; ch [5] =' iLights; ch [6] = 'lumped; ch [7] =' z'

Here I define an array, you can see here new char [9]; I define nine numerical spaces, but only use seven array spaces, giving some leeway

Define the inserted character char c ='w'

This is the character w to be inserted into the array. Let's define it first.

Character order for (int I = 0; I) before print insertion

< ch.length; i++) { System.out.print(ch[i] + " "); } 这是输出一下没有插入w之前的数组值与插入后形成对比 假设插入位置// index为假设字符要插入的位置int index = ch.length; 这里将插入位置放在数组的最大长度处 找到插入位置 // 通过for循环找到要插入的位置 for (int j = 0; j < ch.length; j++) { if (c < ch[j]) { index = j; break; } } 这里利用for循环将index放在合适的位置,char是可以用来比较的,这里用 c < ch[i]的目的就是让这个"w"按照26字母顺序排列,不能让"w"出现在"x"的后面 // 打印要插入的位置看一下是否有问题 // System.out.println(index);数组数据下移// 这是很重要的一步将数组的数据进行下移 for (int k = ch.length - 1; k >

Index; KMI -) {ch [k] = ch [k-1];}

Here, a lot of people ask, why does length want-1? Because length is the length of the array, and the subscript starts at 0, which is 1 less than the length, so-1, we have previously defined the position of index, that is, the position of inserting w, we let k > index, this is because we only need to move the letter after the inserted "w" backward, and the one in front of it doesn't need to move!

Move into the numeric value / / move the data down and insert the data in the position vacated. Ch [index] = c

Assign the value of c to the subscript of array index

Output array / / View the inserted array data for (int l = 0; l < ch.length; lumped +) {System.out.print (ch [l] + ");}

Using for loop to output array

At this point, we insert the "w" in 26 alphabetical order.

Let's see how it works.

At this point, the study of "the method step of inserting a character in the Java array" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report