当前位置:网站首页>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边栏推荐
- Design and implementation of general interface open platform - (39) simple and crude implementation of API services
- 【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
- [algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
- Guided package method in idea
- Devops' future: six trends in 2022 and beyond
- [算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
- 记录:初次cmd启动MySQL拒接访问之解决
- FairyGUI按钮动效的混用
- In 2020, the average salary of IT industry exceeded 170000, ranking first
- FairyGUI循環列錶
猜你喜欢
Mixed use of fairygui button dynamics
[algorithm] sword finger offer2 golang interview question 2: binary addition
Teach you to release a DeNO module hand in hand
Guided package method in idea
[算法] 劍指offer2 golang 面試題2:二進制加法
《软件测试》习题答案:第一章
Code example of MATLAB reading GNSS observation value o file
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
[算法] 剑指offer2 golang 面试题2:二进制加法
341. Flatten nested list iterator
随机推荐
IText 7 generate PDF summary
PRIDE-PPPAR源码解析
Excel导入,导出功能实现
Role movement in the first person perspective
Naive Bayesian theory derivation
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
Code example of MATLAB reading GNSS observation value o file
GNSS positioning accuracy index calculation
How to improve the deletion speed of sequential class containers?
[GNSS data processing] Helmert variance component estimation analysis and code implementation
Solution to the problem of automatic login in Yanshan University Campus Network
Unity3d, Alibaba cloud server, platform configuration
错误:排序与角标越界
KF UD decomposition pseudo code implementation advanced [2]
最短Hamilton路径 (状压DP)
[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
rtklib单点定位spp使用抗差估计遇到的问题及解决
[algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
1041 be unique (20 points (s)) (hash: find the first number that occurs once)
Implementation of Excel import and export functions