当前位置:网站首页>错误:排序与角标越界
错误:排序与角标越界
2022-07-06 09:18:00 【非风之想】
错误提示:
java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 8
java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 8
-1,8索引超出了数组索引(如果用i–,j–就可能出现-1角标越界)
代码如下:
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);
}
//冒泡排序
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]);
}
}
//选择排序
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]);
}
}
//互换元素
public static void swap(String[] arr,int i,int j)
{
String temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
遍历并打印数组
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]+"]");
}
}
}
//选择排序:按索引顺序每次拿出一个元素与其之后的每个元素挨个比较
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]);
}
}
外循环:i到arr.length-1,共arr.length-2次排序,也就是把0到倒数第二个元素每个都拿出来一次,并与其后面的元素比较当然这里是内循环的事了,因为最后一个元素之后没有元素,所以是0<arr.lengrh-1。arr[i]就是外循环每次拿出的元素。
内循环:j到arr.length,每拿出来一个元素都要与其之后的元素比较,所以j=i+1,外循环每加一次,内循环比较的个数就少一个,依次类推,所以外循环拿出来的元素每次都会与最后一个比较,直到倒数第二个元素与最后一个元素比较,完成排序。
小心角标越界,如果拿arr[i]是最后一个元素,还与arr[j]比较就会越界
//冒泡排序:每趟排出一个元素的位置,相邻索引比较
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]);
}
}
外循环:外循环控制总趟数,每趟排出一个元素,依次类推,当排剩下最后一个元素,没有与其比较的元素,所以就是arr.length-1躺排序
内循环:内循环控制每趟排序,arr[j]与arr[j+1]比较,每两个相邻的元素都要比较一次,第一趟排完最大(或最小)的元素就会放到最后一个 位置arr.length,依次类推,最后一趟第一个元素arr[0]与第二个元素arr[1]比较,所以每趟比较的次数为arr.length-1-i次。
小心角标越界,如果拿arr[j]是最后一个元素,还与arr[j+1]比较就会越界边栏推荐
- [leetcode622] design circular queue
- Easy to use shortcut keys in idea
- 【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
- [算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
- Containers and Devops: container based Devops delivery pipeline
- [algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
- 1041 be unique (20 points (s)) (hash: find the first number that occurs once)
- idea中好用的快捷键
- 【无标题】
- Derivation of logistic regression theory
猜你喜欢

Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY

idea问题记录

(core focus of software engineering review) Chapter V detailed design exercises

Unity场景跳转及退出

Fabrication d'un sac à dos simple fairygui

The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan

Unity3d makes the registration login interface and realizes the scene jump

KF UD分解之UD分解基础篇【1】

idea中好用的快捷键

Derivation of logistic regression theory
随机推荐
VLSM variable length subnet mask partition tips
[offer78] merge multiple ordered linked lists
[algorithm] sword finger offer2 golang interview question 1: integer division
MySQL error warning: a long semaphore wait
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
Database table splitting strategy
FairyGUI增益BUFF数值改变的显示
[leetcode622] design circular queue
Agile development helps me
Database course design: college educational administration management system (including code)
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
MySQL performance tuning - dirty page refresh
Lean product development - Lean Software Development & lean product development
How to improve the deletion speed of sequential class containers?
There is no red exclamation mark after SVN update
2021.11.10 compilation examination
Unity scene jump and exit
Acwing-116 pilot brother
【干货】提升RTK模糊度固定率的建议之周跳探测