当前位置:网站首页>Review of some classic exercises of arrays
Review of some classic exercises of arrays
2022-07-25 06:11:00 【Eyes of years】
Some classic exercises of arrays
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
/**1. Array element lookup ( Finds the index of the first occurrence of the specified element in the array ) * If exist : Returns the subscript of this element * If it does not exist : return -1 * * Parameters : int[] arr,int i * Return value : Indexes int * 2. Array element removal 0 Elements * Original array : 4,2,8,0,1,5,0,7,10 * Go to 0 after : 4,2,8,1,5,7,10 */
public class Example01 {
public static void main(String[] args) {
int [] arr={
10,20,30,10,20,0};
System.out.println(SearchNum(arr,20));
char[] CharArr={
'A','B','C','D','E'};
System.out.println(Arrays.toString(ChangeLetter(CharArr)));
System.out.println(Arrays.toString(CheckZero(arr)));
testEmp();
}
//1. Define a method to find the index of the first occurrence of array elements , There is no return -1, There is a return index
// 1) Method has return value --> Returns an index value 2) The array to find for the parameters of the method 、 Looking for elements
public static int SearchNum(int [] arr,int num){
// Traverse the array to find
for (int i = 0; i <arr.length; i++) {
if (arr[i]==num){
return i;
}
}
return -1;
}
//2. Define a method to remove zero elements from an array
/** Ideas :1) Find the number of non-zero elements in the original array * 2) Define a new array to store elements containing zeros * 3) Traverse the original array and copy it to the new array * 4) Return a new array --->int [] * Method name :checkZero * */
public static int[] CheckZero(int [] arr){
//1. Find the number of non-zero elements in the original array
int counter=0;
for (int i = 0; i <arr.length; i++) {
if (arr[i]!=0){
counter++;
}
} if(counter==arr.length){
// If the value of the counter is equal to the length of the array, the original array is returned , Note that the original array does not contain zero
return arr;
}
//2. Define a new array
int [] newArr=new int[counter];
//3. Iterate over the original array , Copy to a new array
//i As the original array index , j Index as new array
int j=0;
for (int i = 0; i <arr.length ; i++) {
if(arr[i]==0){
newArr[j++]=arr[i];
}
}
// Return a new array
return newArr;
}
//3. Convert an array containing uppercase letters into an array containing lowercase letters and return
// Method name ChangeLetter ---> The parameter of the method is a character array , The return value is an array of characters
public static char[] ChangeLetter(char[] arr){
if(arr!=null && arr.length==0){
// If the original array is not empty and the length is not equal to zero
for (int i = 0; i < arr.length-1; i++) {
if(arr[i]>='A' && arr[i]<='Z'){
arr[i] += 32;
}
}
}
return arr;
}
/** 4. practice : Define employee types , Store data of multiple employee types 1. Display and output all employee information 2. Find the names of all employees and store them 3. Find all salaries >10000 The employees' -> layoffs 4. Find the number of employee age statistics */
public static void testEmp(){
Employee[] emps = {
new Employee(1001," Zhang ziyi ",32,15000),
new Employee(1002," Mr Wang ",42,18000),
new Employee(1003," Jack ma, ",32,5000)
};
//1. Display and output all employee information
System.out.println(Arrays.toString(emps));
// 2. Find the names of all employees and store them
// The new array stores the names of all employees
String [] names=new String[emps.length];
for (int i = 0; i <emps.length ; i++) {
names[i]=emps[i].getName();
}
System.out.println(Arrays.toString(names));
// 3. Find all salaries >10000 The employees' -> layoffs
//ArrayList Dynamically add and delete according to the amount of data Store multiple data , Store any type of data
//1) A common set , For polymorphic data
ArrayList ls = new ArrayList();
//2) Iterate over the original array , Find all salaries <=10000 Of employees into the collection ls
for(Employee emp:emps){
if(emp.getSalary()<=10000){
ls.add(emp);
}
}
//3) Print ls Data in
System.out.println(ls);
//4. Find the number of employee age statistics
HashSet set = new HashSet(); // Store the age of the employee who appears
//HashSet It can be de duplicated according to the data , Duplicate data cannot be added to the collection repeatedly , Finally get the number of data in the set
for(Employee emp:emps){
set.add(emp.getAge());
}
System.out.println(set.size()); // Number of data in the set
}
}
边栏推荐
- Sword finger offer 36. binary search tree and bidirectional linked list
- Dynamic planning learning notes
- Unity animator animation and state machine
- For data security reasons, the Dutch Ministry of Education asked schools to suspend the use of Chrome browser
- Calculate BDP value and wnd value
- This is how the permission system is designed, yyds
- What does PK, NN, Qu, B, UN, ZF, AI, G mean when creating tables in MySQL
- UML modeling tools Visio, rational rose, powerdesign
- sqlilabs less-29
- How to start if you want to be a product manager?
猜你喜欢

Promise implementation

深度解析:2022年最火的商业模式链动2+1,是合法模式吗?

Sword finger offer 36. binary search tree and bidirectional linked list

(牛客多校二)J-Link with Arithmetic Progression(最小二乘法/三分)

(2022 Niuke multi school) D-Link with game glitch (SPFA)

Baidu, Alibaba, Tencent, who fell first?

(2022牛客多校二)L-Link with Level Editor I(动态规划)

Brief introduction of acoustic filter Market

npx和npm区别

HTB-Devel
随机推荐
The computer accesses the Internet normally with the same network cable, and the mobile phone connects to WiFi successfully, but it cannot access the Internet
PMP Exam is easy to confuse concept discrimination skills! Don't lose points after reading!
(16) [system call] track system call (3 rings)
R language uses data.table function to create data.table data (use: operator to create continuous numeric vector)
【每日一练】day(14)
EOL offline sequence based on iso13209 (Otx)
10. Rendering Basics
Leetcode/ number of 1 in the first n digit binary
有什么能在网上挣钱的项目啊?做自媒体靠谱吗?
Please stop using system The currenttimemillis() statistical code is time-consuming, which is really too low!
SAP FICO 第三节 BDC和LTMC导入S4财务科目
Prometheus operator configures promethesrule alarm rules
(15)[驱动开发]过写拷贝
计算BDP值和wnd值
Summer summary 2
日期(DAY 76)
【Unity3D】UGUI回调函数
MFC IniFile Unicode mode reading method
Singing "Seven Mile fragrance" askew -- pay tribute to Jay
sqlilabs less-29