当前位置:网站首页>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边栏推荐
- Office提示您的许可证不是正版弹框解决
- What are the advantages of using SQL in Excel VBA
- 记录:newInstance()过时的代替方法
- Matlab读取GNSS 观测值o文件代码示例
- [algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
- [algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
- FairyGUI循環列錶
- Fairygui loop list
- [algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
- (the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
猜你喜欢

Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance

FairyGUI人物状态弹窗

抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现

Design and implementation of general interface open platform - (39) simple and crude implementation of API services
![[GNSS data processing] Helmert variance component estimation analysis and code implementation](/img/4e/ff0334cf9a2a37096778a8c5719a4e.jpg)
[GNSS data processing] Helmert variance component estimation analysis and code implementation

染色法判定二分图

Matlab读取GNSS 观测值o文件代码示例
![[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity](/img/9d/7284c1399964d3fb48886f12e4941c.jpg)
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity

Guided package method in idea

Edit distance (multi-source BFS)
随机推荐
基于rtklib源码进行片上移植的思路分享
[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
Unity3d, Alibaba cloud server, platform configuration
记录:Navicat Premium初次无法连接数据库MySQL之解决
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
[rtklib 2.4.3 B34] version update introduction I
GNSS定位精度指标计算
FairyGUI循環列錶
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
Realization of the code for calculating the mean square error of GPS Height Fitting
What are the advantages of using SQL in Excel VBA
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
Fairygui gain buff value change display
Knowledge system of digital IT practitioners | software development methods -- agile
Containers and Devops: container based Devops delivery pipeline
There is no red exclamation mark after SVN update