<p> </p>
<p><img alt="" height="360" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-52093e8f21962dedf0213fa40a8e2a20.png" width="1200"></p>
<pre class="blockcode"><code class="language-java">import java.util.Scanner;
class Demo05_01_02{
public static void main(String[] args){
int[] arr=new int[101];
Scanner scanner=new Scanner(System.in);
System.out.print("Enter numbers:");
while(true){
int num=scanner.nextInt();
if(num==0){
break;
}
arr[num]++;
}
for(int i=0;i<arr.length;i++){
if(arr[i]!=0){
System.out.println(i+" occurs "+arr[i]+(arr[i]>1?" times":" time"));
}
}
}
}</code></pre>
<p> </p>
<p><img alt="" height="270" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-0ab70c1918f5638dbd6627d3dfe539ec.png" width="862"></p>
<pre class="blockcode"><code class="language-java">import java.util.*;
class Demo05_02{
public static void main(String[] args){
/*
思路1
在全部输入之后去重复 func1
思路2
边输入边去重复 func2
*/
// func1();
func2();
}
public static void func2(){
int[] arr=new int[0];
Scanner scanner = new Scanner(System.in);
System.out.print("Enter numbers:");
for(int i=0;i<10;i++){
int num=scanner.nextInt();
if(!contains(arr,num)){
arr=copyOf(arr,arr.length+1);
arr[arr.length-1]=num;
}
}
System.out.println(Arrays.toString(arr));
}
public static void func1(){
//1.循环遍历数组进行赋值
Scanner scanner = new Scanner(System.in);
System.out.print("Enter numbers:");
int[] arr = new int[10];
for(int i = 0;i < arr.length;i++){
arr[i] = scanner.nextInt();
}
//2.开始对已有的数据进行去重复操作
// 1 2 3 3 2 4 3 2 4 1
// 1 2 3 4
// method1(arr); //空间S(n) 时间O(nm)
// method2(arr); //空间S(1) 时间O(n^2)
// method3(arr);
}
public static void method3(int[] arr){
//不创建额外空间 不许改变原先的顺序
int i=0;
int size=arr.length;
while(i<size){
for(int j=i+1;j<size;){
if(arr[j]==arr[i]){
for(int k=j+1;k<size;k++){
arr[k-1]=arr[k];
}
size--;
}else{
j++;
}
}
i++;
}
for(i=0;i<size;i++){
System.out.print(arr[i]+" ");
}
}
public static void method2(int[] arr){
//插入排序
for(int i=1;i<arr.length;i++){
int e=arr[i];
int j;
for(j=i;j>0&&arr[j-1]>e;j--){
arr[j]=arr[j-1];
}
arr[j]=e;
}
//连续相等
for(int i=0;i<arr.length;){ //O(n)
System.out.print(arr[i]+" ");
int count=1;
for(int j=i+1;j<arr.length;j++){
if(arr[j]==arr[i]){
count++;
}else{
break;
}
}
i+=count;
}
}
public static void method1(int[] arr){
int[] newArr=new int[0];
for(int i=0;i<arr.length;i++){ //O(n)
if(!contains(newArr,arr[i])){ //O(m)
newArr=copyOf(newArr,newArr.length+1);
newArr[newArr.length-1]=arr[i];
}
}
System.out.println(Arrays.toString(newArr));
}
public static boolean contains(int[] arr,int key){
for(int i=0;i<arr.length;i++){
if(arr[i]==key){
return true;
}
}
return false;
}
public static int[] copyOf(int[] arr,int newLen){
int[] newArr=new int[newLen];
for(int i=0;i<arr.length;i++){
newArr[i]=arr[i];
}
return newArr;
}
}</code></pre>
<p>5.3 </p>
<p> </p>
<p><img alt="" height="350" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-99f61fd8b9c5c00276174396caf9fa7d.png" width="1200"></p>
<pre class="blockcode"><code class="language-java">import java.util.*;
class Demo05_03{
public static void main(String[] args){
//1.获取用户的输入 只不过第一个输入的数据时数据的个数(数组的长度)
Scanner scanner=new Scanner(System.in);
System.out.print("Enter a list:");
int len=scanner.nextInt();//获取的第一个数值就是数组的长度
int[] arr=new int[len];
for(int i=0;i<arr.length;i++){
arr[i]=scanner.nextInt();
}
//2.对数组进行有序的判断
if(isSorted(arr)){
System.out.println("The list is already sorted.");
}else{
|
|