我正在尝试为Java中的arraylists创建一个bubble swap方法,但是我遇到了一个错误.这是我的代码:
public static void BubbleSort()
{
list.remove("");
for (int i = list.size(); i > 0; i--)
{
for (int j=0; j < i; j++)
{
if(Integer.valueOf((String) list.get(j)) < Integer.valueOf((String) list.get(j + 1)))
Swap(list.get(j), list.get(j + 1));
}
}
System.out.println(list);
}
Run Code Online (Sandbox Code Playgroud)
这是它调用的Swap方法:
public static void Swap(Object object, Object object2)
{
Object spotC = object;
list.set(list.indexOf(object), object2);
list.set(list.indexOf(object2), spotC);
}
Run Code Online (Sandbox Code Playgroud) java sorting arraylist bubble-sort indexoutofboundsexception
最近在使用 arrayList 时,我发现了一个奇怪的问题。我想在数组列表中保留 2 个元素,并希望在元素 0 中保留 object1,在 element1 中保留 object2。
我在循环中决定这个。
当它碰巧添加第一个元素时,它会抛出 indexOutofBounds。根据java doc,因为索引大于大小,所以这样做。
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
list.add(1,"SECONDITEM"); // throwing due to size is not set
list.add(0,"FIRSTITEM");
int postn=0;
for(String item:list){
System.out.println("Position "+postn+" :"+item);
postn++;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试将其他项目设置为 0,1 元素中的占位符并尝试做同样的事情
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
list.add(0,"x");
list.add(1,"y");
list.add(1,"SECONDITEM");
list.add(0,"FIRSTITEM");
int postn=0;
for(String item:list){
System.out.println("Position "+postn+" :"+item);
postn++;
}
}
Output:
Position 0 :FIRSTITEM
Position 1 …Run Code Online (Sandbox Code Playgroud) 我不明白为什么这会超出范围,你能帮助我吗?
会发生什么:
出于某种原因,在列表中的第一项之后,它超出范围,但我不知道为什么.
import java.util.Scanner; //import scanner
public class project2 {
public static void main (String[] args){
Scanner input = new Scanner(System.in); //scanner for input
int a = 0;
double [] lista = new double [a]; //create array
double [] listb = new double [a]; //create array
System.out.println("How long are the lists? ");
System.out.print("(The lists should be the same length): ");
a = input.nextInt();
int count=1;
System.out.println("Enter numbers for list A:");
for(int j = 0; j < a-1 …Run Code Online (Sandbox Code Playgroud) 我试图解决基于回文的问题.
但我一直得到的是这个例外 - ArrayIndexOutOfBoundsException
我真想自己想出来,但我无法理解原因.
这是代码:
public class LargestPalindrome
{
public int isPalindrome(int n)
{
int revNum=0;
int num=n,flag=0;
while(num!=0)
{
revNum=revNum*10;
revNum=revNum + (num%10);
num=num/10;
}
if(n==revNum)
flag=1;
else
flag=0;
return(flag);
}
public static void main(String args[])
{
LargestPalindrome p=new LargestPalindrome();
int flag=0,pro=0;
int a[]= new int[100];
int b[]=new int[100];
for(int i=1;i<=99;i++)
{
a[i]=i;
b[i]=i;
}
for(int i=1;i<=99;i++)
{
for(int j=1;j<=99;i++)
{
pro=a[i]*b[j];
flag=p.isPalindrome(pro);
if(flag==1)
System.out.println(pro);
pro=0;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我一直存在的问题是它说java.lang.ArrayIndexOutOfBoundsException:165
165是它实际应包含的值的数量.
我尝试将整数数组的长度更改为更大的数字,但它一直给我错误,无论是更大的数字.
我把所有内容都打印出来,看看问题是否可能是我的for循环,但是那里的一切似乎都很好(165个阵列中有正确的数字)
我看不出问题出在哪里.
public int[][] getTilePositionsIn(int pixelLeft, int pixelBottom, int pixelRight, int pixelTop){
int starterTileX = pixelLeft/getTileLength();
if (starterTileX < 0)
starterTileX = 0;
System.out.println(starterTileX);
int starterTileY = pixelBottom/getTileLength();
if (starterTileY < 0)
starterTileY = 0;
System.out.println(starterTileY);
int tileLength = getTileLength();
int blockWidth = pixelRight - pixelLeft + 1;
int widthInTiles = blockWidth/tileLength +1;
int blockHeight = pixelTop - pixelBottom + 1;
int heightInTiles = blockHeight/tileLength +1;
int numberOfTiles = widthInTiles * heightInTiles;
System.out.println(widthInTiles);
System.out.println(heightInTiles);
System.out.println(numberOfTiles);
int[][] tiles …Run Code Online (Sandbox Code Playgroud) 当我试图打印出我的2维String数组时,我收到一条错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at MainClass.main(MainClass.java:51)
Run Code Online (Sandbox Code Playgroud)
这是我的阵列:
String[][] list = {
{"1",null},
{"2",null},
{"3",null},
{"4",null},
{"5",null},
{"6",null},
{"7",null},
{"8",null},
{"9",null},
{"10",null},
{"11",null},
{"12",null},
{"13",null},
{"14",null},
{"15",null}
};
Run Code Online (Sandbox Code Playgroud)
这就是我打印它的方式:
for( int row=0; row<list.length; row++) {
for( int col=0; col<list.length; col++) {
System.out.print(list[row][col] + "\t"); //this is MainClass:java:51 where the error is happenin
}
System.out.println();
}
Run Code Online (Sandbox Code Playgroud)
我试图以漂亮的网格形状打印出来.我现在这样做的方式是为Integr数组而工作,这让我感到很困惑.
java eclipse arrays multidimensional-array indexoutofboundsexception
在cppreference上,我读到了以下内容std::vector<T,Allocator>::operator[]:
通过此运算符访问不存在的元素是未定义的行为。
标准草案中是否有类似的句子?或者说,我应该从标准的哪一部分推断出这个东西?
我想如果标准对此只字未提,那就会成为 UB。但我还没有找到任何相关信息std::vector<T,Allocator>::at,所以...
c++ std undefined-behavior language-lawyer indexoutofboundsexception
这里有轻微的c#问题.我目前正在使用Unity编写一个小型平台游戏,我有一些用于检查碰撞的光线投射等.
现在,我开始通过将那些光线投射的结果存储到整数数组中来清理代码,但是我得到了一个IndexOutOfRangeException.
我试图多次阅读我的代码,但似乎无法找到导致问题的原因.如果有人可以帮助我,我会很高兴.
这是我的代码:
using UnityEngine;
using System.Collections;
public class PlayerRayCaster : MonoBehaviour {
public float playerHeight = 1;
public enum FeetState {Air, Ground};
public FeetState playerFeetState = FeetState.Air;
public int feetHitRays;
public int behindHitRay;
//Arrays of rays. value of 1 means that ray hits a target, 0 means that it does not hit.
public int[] sideRays; // [0-3] = Left side. [4-8] = Right side.
public int[] depthRays; // [0] = Away from camera. [1] = Towards camera.
public …Run Code Online (Sandbox Code Playgroud) 我不知道为什么这个方法抛出一个ArrayIndexOutOfBounds异常.
When I change the initial "high"价值,我搜索"int high = array.length - 1;"的程序return any integer value.
我究竟做错了什么?
提前致谢!
public class BinarySearch {
public static void main(String[] args) {
int searchValue = 12;
int[] givenNums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
binarySearch(givenNums, searchValue);
System.out.println("\nResult: " + searchValue);
}
public static int binarySearch(int[] array, int key) {
int low = 0;
int high = array.length;
int mid = (low + high) …Run Code Online (Sandbox Code Playgroud) java methods exception binary-search indexoutofboundsexception
我似乎无法理解以下错误.
列表框超出范围(0)TString
我有一个带有列表框的表单或窗口,以下代码应该可以使用它.它假设从ini文件中获取字符串列表并设置为列表框.
IF selectedbox1count <> 0 then
BEGIN
FOR i:=0 to selectedbox1count-1 do
selectedbox.items[i] := AInifile.ReadString('DATAVIEW2', 'SHIFT1CHART'+(i+1), ' ');
END;
Run Code Online (Sandbox Code Playgroud)
但它总是在第一个实例到达该selectedbox.items[i]行时弹出一条错误消息.读取ini文件将返回"NEW CHART 2"字符串.我在这里错过了什么吗?
更新:selectedbox1count保存ini文件中的值...
delphi listbox tstringlist lazarus indexoutofboundsexception