我想制作一个数组的二维数组,每个数组都填充另一个对象.到目前为止我所拥有的是:
class CustomCache{
boolean dirty = false;
int age = 0;
String addr;
public CustomCache(boolean a, String b, int c){
dirty = a;
addr = b;
age = c;
}
}
class Setup {
int wpb;
CustomCache[] wpbArray = new CustomCache[wpb];
public Setup(int a){
wpb = a;
}
}
Run Code Online (Sandbox Code Playgroud)
Setup[][] array = new Setup[numSets][numBlocks];
for(int i=0; i<numSets; i++){
for(int j=0; j<numBlocks; j++){
array[i][j] = new Setup(wpb);
for(int k=0; k<wpb; k++){
array[i][j].wpbArray[k] = new CustomCache(false, "", 0);
}
}//end inner for …Run Code Online (Sandbox Code Playgroud) 嗨,大家好,我在夏天学习java,这是最后的任务,我被困住了.该程序应该输入我输入的13个数字,对它们进行排序,然后找到我在原始数组中输入的最大数字的索引号.我试图看看我的选择方法是否有效,但每当我尝试输入数字时,我都会得到一个超出范围的错误.这有点令人沮丧,我一直试图找到并回答几个小时.任何帮助将不胜感激.谢谢!
import java.util.Scanner;
public class fmax
{
public static void main(String[] args)
{
int indmax;
int[] fmax = new int[13];
fillmax(fmax);
//System.out.println(fmax);
indmax = maxfmax(fmax);
//indmin = minfmax();
System.out.println(indmax);
}
public static void fillmax(int[] farray)
{
Scanner sc = new Scanner(System.in);
int i = 0;
for(i = 0; i < farray.length; i++)
{
farray[i] = sc.nextInt();
}
}
public static int maxfmax(int[] farray)
{
int[] copy = farray;
int j, x=0, i;
boolean flag = true;
int temp;
while(flag)
{ …Run Code Online (Sandbox Code Playgroud) 我在分配头部的行上遇到以下异常,我不确定原因:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 44
当使用"{1,2},3"作为s运行调试器的值时,我可以跟随循环,并且正确地将其分配commaSpot为5,因为我认为它应该是.但由于某种原因s.charAt(commaSpot)),即使s长度为7个字符,也不认为5是有效索引.
int commaSpot = 0;
int bracePairs = 0;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == '{') bracePairs++;
if(s.charAt(i) == '}') bracePairs--;
if(s.charAt(i) == ',' && bracePairs == 0) {
commaSpot = i;
break;
}
}
head = s.substring(0, s.charAt(commaSpot));
tail = s.substring(s.charAt(commaSpot), s.length());
Run Code Online (Sandbox Code Playgroud) 我正在尝试为动态壁纸创建一个2D正方形阵列(自定义类).我计算了使用屏幕宽度和高度以及方形边长和它们之间的间隙距离的最终整数可以显示的数量.初始化数组中的方块时,我使用嵌套for循环.但是,当我在模拟器上运行它时,我得到一个超出范围的索引.
java.lang.ArrayIndexOutOfBoundsException:length = 6; 指数= 6
为什么for循环不起作用?
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
this.width = width;
this.height = height;
calcSquares();
initSquares();
super.onSurfaceChanged(holder, format, width, height);
}
private void calcSquares()
{
numSquaresW = width/SQUARE_SIDE;
numSquaresH = height/SQUARE_SIDE;
while(width % (numSquaresW * SQUARE_SIDE) < (numSquaresW + 1) * SQUARE_GAP)
numSquaresW--;
while(height % (numSquaresH * SQUARE_SIDE) < (numSquaresH + 1) * SQUARE_GAP)
numSquaresH--;
marginW = ((width % numSquaresW) - ((numSquaresW - 1) * SQUARE_GAP)) / 2;
marginH …Run Code Online (Sandbox Code Playgroud) java android for-loop live-wallpaper indexoutofboundsexception
所以我有下面的代码使用orgTxt和创建一个加密的字符串rndTxt,当我在Visual Studio中调试代码时,我在第二个for循环中得到IndexOutOfRangeException的错误.
我用断点检查索引的值,它似乎完全在范围内,任何人都知道问题是什么?如果需要更多信息来帮助解决此错误,请留下评论.
//variables
string scrtTxt = null;
string rndTxt = null;
string orgTxt = reader.ReadToEnd();
//assigning random a string from key (set of all capital letters) to rndTxt
for (int i = 0; i < fileInfo.Length; i++)
{
rndTxt += key[random.Next(0, key.Length)];
}
//generating the encrypted message scrtTxt
int j = 0;
for (int i = 0; i < fileInfo.Length; i++)
{
if ((orgTxt[i] + rndTxt[j] - 'A') <= 'Z' && (orgTxt[i] + …Run Code Online (Sandbox Code Playgroud) 我正在尝试关注精灵动画的youtube教程并且一直遇到通常与之相关的错误activity.这是一个:
java.lang.RuntimeException:无法启动活动ComponentInfo {rocketanimalcontrol.reap/rocketanimalcontrol.reap.MainActivity}:java.lang.IndexOutOfBoundsException:索引0无效,大小为0
罪魁祸首是在下面的某个地方吗?
MainActivity.java
package rocketanimalcontrol.reap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity; //? This needed with AppCompatActivity ?
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.view.View;
import android.view.Menu;
//import android.view.animation.Animation;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<Bitmap> bitmaps;//new here or in OnCreate?
private SpriteAnimation animation;
private CustomView view;
//Auto-generated code
// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
// }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view …Run Code Online (Sandbox Code Playgroud) java android runtime indexoutofboundsexception android-bitmap
我对Java很新,我有一些代码可以在一个循环中作用于数组,并且数组会改变每个循环.当数组太短时,有时代码会抛出IndexOutOfBoundsException.如果没有抛出异常,我有没有办法只执行代码?我知道在Javascript中我可以做类似的事情
var a = arr[index];
if (!a === null) { //do something }
Run Code Online (Sandbox Code Playgroud)
如何在Java中实现相同的效果?
当我运行这个应该检测图像中的圆圈的程序时,当我调用drawCircles()时,我在最后一个方法中得到了数组索引超出范围的异常
findCircle()读取图像并搜索圆圈,drawCircles()使用Bresenaham算法绘制它们.
public class Assig1 {
public static void main(String[] args) {
try {
// arg 0 is the input image name
BufferedImage img = ImageIO.read(new File(args[0]));
// arg 1 is the min radius
int minr = Integer.parseInt(args[1]);
// arg 2 is the max radius
int maxr = Integer.parseInt(args[2]);
// if present, arg 3 is the max width we consider
int w = (args.length>3) ? Integer.parseInt(args[3]) : img.getWidth();
// if present, arg 4 is the max height we consider
int …Run Code Online (Sandbox Code Playgroud) 我正在用Java制作一个地下城游戏.我创建了一个将地图存储在2D数组中的方法.该数组如下所示:
[[#, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #],
[#, ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., #],
[#, ., ., ., ., ., ., G, ., ., ., ., ., ., ., ., E, ., #],
[#, ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., ., #],
[#, ., ., …Run Code Online (Sandbox Code Playgroud) java printing arrays multidimensional-array indexoutofboundsexception
在该程序中,用户在命令行中输入两个字符串.其中一个是数字,另一个是字符串.该程序用于根据电话拨号盘上每个按钮上的数字/字母检查输入的数字和字符串是否相同.我得到一个例外,说:
Run Code Online (Sandbox Code Playgroud)Unhandled exception at 0x775DDAE8 in Project10.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0018F158.
非常感谢任何帮助,非常感谢你:)
using namespace std;
bool checkPswd(string keyStrokes, string password) {
string temp;
string temp2;
bool temporary = 1;
string phoneButtons[10] = {
"", ""
"abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"
};
for (int i = 0; i < keyStrokes.length(); i++) {
for (int j = 2; j < 10; j++) {
for (int k = 0; k < phoneButtons[j].length(); k++) {
temp = …Run Code Online (Sandbox Code Playgroud)