当前位置:网站首页>Error: sorting and subscript out of bounds
Error: sorting and subscript out of bounds
2022-07-06 12:59:00 【Non wind thought】
Error message :
java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 8
java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 8
-1,8 Index exceeds array index ( If you use i–,j– It's possible -1 The corner marker is out of bounds )
The code is as follows :
public class StringTest_1
{
public static void main(String[] args)
{
String[] arr={
"abc","NBA","CBA","java","Hong","demo","qq","WX"};
printArray(arr);
sortString(arr);
printArray(arr);
maopaoString(arr);
printArray(arr);
}
// Bubble sort
public static void maopaoString(String[] arr)
{
for(int i=0;i<arr.length-1;i++)
for(int j=0;j<arr.length-1-i;j++)
{
if(arr[j].compareTo(arr[j+1])>0)
swap(arr,j,j+1); //swap(arr,arr[j],arr[j+1]);
}
}
// Selection sort
public static void sortString(String[] arr)
{
for(int i=0;i<arr.length-1;i++)
for(int j=i+1;j<arr.length;j++)
{
if(arr[i].compareTo(arr[j])>0)
swap(arr,i,j);//swap(arr,arr[i],arr[j]);
}
}
// Exchange elements
public static void swap(String[] arr,int i,int j)
{
String temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
Traverse and print the array
public static void printArray(String[] arr)
{
System.out.print("[");
for(int i=0;i<arr.length;i++)
{
if(i!=arr.length-1)
System.out.print(arr[i]+",");
else
System.out.println(arr[i]+"]");
}
}
}
// Selection sort : Take out one element at a time according to the index order and compare it with each subsequent element one by one
public static void sortString(String[] arr)
{
for(int i=0;i<arr.length-1;i++)
for(int j=i+1;j<arr.length;j++)
{
if(arr[i].compareTo(arr[j])>0)
swap(arr,i,j);//swap(arr,arr[i],arr[j]);
}
}
Outer loop :i To arr.length-1, common arr.length-2 Secondary ranking , That is the 0 Take out each of the penultimate elements once , And compared with the following elements, of course, here is the matter of inner loop , Because there is no element after the last element , So it is 0<arr.lengrh-1.arr[i] It's the elements taken out by the outer loop every time .
Inner loop :j To arr.length, Each element you take out should be compared with its subsequent elements , therefore j=i+1, Every time the external circulation is added , The number of internal loop comparisons is one less , By analogy , So the elements from the outer loop will be compared with the last one every time , Until the penultimate element is compared with the last element , To complete the order .
Be careful that the corner marker is out of bounds , If I take arr[i] It's the last element , Also with arr[j] The comparison will cross the line
// Bubble sort : The position of one element per trip , Adjacent index comparison
public static void maopaoString(String[] arr)
{
for(int i=0;i<arr.length-1;i++)
for(int j=0;j<arr.length-1-i;j++)
{
if(arr[j].compareTo(arr[j+1])>0)
swap(arr,j,j+1); //swap(arr,arr[j],arr[j+1]);
}
}
Outer loop : Total number of external circulation control trips , One element per trip , By analogy , When the last element is left in the row , There are no elements to compare with , So is arr.length-1 Lying order
Inner loop : The inner loop controls the sequencing of each trip ,arr[j] And arr[j+1] Compare , Every two adjacent elements should be compared , The first row is the largest ( Or the smallest ) The element of will be placed in the last Location arr.length, By analogy , The first element of the last trip arr[0] With the second element arr[1] Compare , So the number of comparisons per trip is arr.length-1-i Time .
Be careful that the corner marker is out of bounds , If I take arr[j] It's the last element , Also with arr[j+1] The comparison will cross the line边栏推荐
- Wechat applet development experience
- Game 280 weekly
- KF UD分解之UD分解基础篇【1】
- The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
- Sharing ideas of on-chip transplantation based on rtklib source code
- RTKLIB: demo5 b34f. 1 vs b33
- Office提示您的许可证不是正版弹框解决
- GNSS positioning accuracy index calculation
- MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
- What are the functions and features of helm or terrain
猜你喜欢
[算法] 剑指offer2 golang 面试题10:和为k的子数组
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
Unity3d, Alibaba cloud server, platform configuration
编辑距离(多源BFS)
Guided package method in idea
Detailed explanation of balanced binary tree is easy to understand
FGUI工程打包发布&导入Unity&将UI显示出来的方式
FairyGUI人物状态弹窗
341. Flatten nested list iterator
随机推荐
NovAtel 板卡OEM617D配置步骤记录
MySQL error warning: a long semaphore wait
Teach you to release a DeNO module hand in hand
Liste des boucles de l'interface graphique de défaillance
FairyGUI按钮动效的混用
[algorithm] sword finger offer2 golang interview question 10: subarray with sum K
121道分布式面试题和答案
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
Meanings and differences of PV, UV, IP, VV, CV
Force buckle 1189 Maximum number of "balloons"
Guided package method in idea
GNSS positioning accuracy index calculation
C programming exercise
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
Prove the time complexity of heap sorting
wsl常用命令
堆排序【手写小根堆】
1041 be unique (20 points (s)) (hash: find the first number that occurs once)
【GNSS】抗差估计(稳健估计)原理及程序实现