Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
sakurajh
java
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
java
/
JavaExtend05.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
41 lines (35 loc) · 1.82 KB
main
Breadcrumbs
java
/
JavaExtend05.java
Copy path
Top
File metadata and controls
Code
Blame
41 lines (35 loc) · 1.82 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.baidou;
import java.util.*;
public class JavaExtend05 {
//交换函数,与JavaBasic22中四、利用数组完全一样。
//定义为静态方法
//如果不定义为静态方法,则在主函数中需要依赖于对象,需要通过对象来调用
public static void swap(int[] arr,int num1,int num2){
int temp = arr[num1];
arr[num1] = arr[num2];
arr[num2] = temp;
}
//本题的关键在于找出给出的int数组的最大元素和最小元素
public static void main(String[] args) {
int num = new Random().nextInt(10); //数组元素的个数随机生成(10以内)
int[] arr = JavaExtend03.getRandomArr(num); //获取一个长度随机,元素内容随机的数组
System.out.print("随机生成的int数组是:");
JavaExtend03.print(arr);
int min = 0,max = 0; //min 和 max 分别是数组中最小值的下标和最大值的下标
for (int i = 0;i < arr.length;i ++) {
if (arr[i] < arr[min])
min = i; //因为最后还要交换最大值与第一个元素,最小值与最后一个元素,故要知道最小值和最大值的下标
if (arr[i] > arr[max])
max = i;
}
System.out.println("数组中的最大值是 " + arr[max] + ",最小值是 " + arr[min]);
//将题目中的交换理解为:先将数组中最大元素与第一个元素交换,然后再将第一次交换后的数组中的最小元素与此时的最后一个元素交换
swap(arr,0,max);
if (min == 0)
swap(arr,arr.length-1,max);
else
swap(arr,arr.length-1,min);
System.out.print("交换后的int数组是:");
JavaExtend03.print(arr);
}
}
You can’t perform that action at this time.