Tag: 数组

生命游戏ArrayIndexOutofBounds

我在做康威的生活游戏。 我很确定我已接近完成,但是当我运行它时,我Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -1 at game.of.life.GameOfLife.generation(GameOfLife.java:77) at game.of.life.GameOfLife.main(GameOfLife.java:32) Java Result: 1获得了Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -1 at game.of.life.GameOfLife.generation(GameOfLife.java:77) at game.of.life.GameOfLife.main(GameOfLife.java:32) Java Result: 1 我假设当检查数组边缘的邻居的方法时,没有任何东西,所以它死了或什么的。 我只是不知道如何做到这一点,所以不会发生。 有人有想法吗? 代码如下。 package game.of.life; import java.util.Scanner; public class GameOfLife { static boolean[][] current = new boolean[10][10]; static boolean[][] old = new boolean[10][10]; static int population = 10; […]

带有generics的地图数组问题

可能重复: Javagenerics和数组初始化 如何在Java中实例化一组地图? 我知道我能做到: Map map = new HashMap(); 所以我应该能够: Map[] maps = new HashMap[10]; 但这不起作用,给出编译问题。

数组中的随机整数

我是Java的新手,我只是在使用多个类并使用System.out.println来练习我的技能。 我正在尝试制作一个与计算机进行“对话”的程序。 我想要尝试做的不是每次运行控制台时具有相同年龄的计算机,而是使用数组列出随机年龄的负载,然后随机选择。 在我的计算机课上,我得到了: static int [] compAge = {19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; 在我的主要会话课上,我有: int Uage = input.nextInt(); // (Uage is user age) System.out.println(“That’s cool! You’re ” +Uage+ “. I’m ” + (Computers age here) + ” myself. Where do you live?”); 我已经阅读了一下,发现了诸如此类的代码 compAge[new Random().nextInt(compAge.length)] 但老实说,我对数组的知识和使用随机函数(我已经导入它)是非常有限的,我不知道该去哪里。 […]

数据库无法检索图像或为空,导致数组错误。 怎么解决?

我的问题是java.lang.IndexOutOfBoundsException:索引0无效,大小为0 。 我不知道修复此错误,我没有发现我的arrays有任何问题。 我是android的新手。 我希望你们都明白。 也许这会导致我的错误值出错。 输入(KEY_IMG,Utility.getBytes(quest.getBitmap())); 。 抱歉,我的英文。 UPDATE 数据库问题导致java.lang.IndexOutOfBoundsException:索引0无效,大小为0 MainActivity.java package com.example.quizjavatest; import java.util.ArrayList; import android.os.Bundle; import android.app.Activity; import java.util.List; import android.content.Intent; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; public class MainActivity extends Activity { List quesList; int score=0; int qid=0; ImageView quizimg; Question currentQ; TextView […]

如何在Java中读取文件字节?

如何从文件中读取一个字节,并将其放入字节数组而不将字节转换为整数?

int 无法转换为int

我想将2D数组用于成绩簿方法,其中包含学生姓名,然后是考试成绩。 在课堂上我有如下。 private int numberOfStudents; private String [] studentName; private int examNumber; private int [] examScores; private int [][] gradebook; public ExamAverage () { numberOfStudents = 0; studentName = new String[numberOfStudents]; examNumber = 0; examScores = new int [numberOfStudents]; gradebook = new int [numberOfStudents][examScores]; 然而我得到一个错误int []无法转换为int。

如何更改数组的长度?

所以我被分配了一个项目,我有一个数组,当用户将元素放入这个数组时,一旦它变满就必须加倍。 我们不允许在集合界面中使用数组列表或任何内容。 我试图做的是在旧数组完成后创建一个新数组,然后我将值复制到新数组。 问题是我不知道有多少次我需要制作一个新arrays,所以我想知道如何解决这个问题。

Java:线程“main”中的exceptionjava.lang.StringIndexOutOfBoundsException:字符串索引超出范围:

我是初学者,任何人都可以帮我弄清楚我们在做什么。 我试图读取一个String并将String的每个字符存储在一个数组中。 import java.util.Scanner; public class CoreMainDigitExtractor { static Scanner inputString = new Scanner(System.in); public static void main(String[] args) { digitExtractor ExtracDig = new digitExtractor(); System.out.println(“Enter a String to Extract and Display Vertically in Reverse Order : “); String input1 = inputString.nextLine(); System.out.println(“User input String is : ” + input1); System.out.println(“Calling Method ‘OrderByMaths’ in Object […]

Java array.length方法

我收到错误:未定义的符号:长度。 import java.util.*; class ssss { public static void main(String args[]) { int a[]={1,2,3,4}; System.out.println(a[1]); int x = a.length(); System.out.println(x); } }

从数组中获取随机值

我有一个简单的应用程序,可以生成事实 我想结合一个生成随机事实而不是递减递减的函数。 我的arrays看起来像这样。 public class Facts { String facts[] = { “Elephants are the only mammals that can’t jump.”, “Candles will burn longer and drip less if they are placed in the freezer a few hours before using.”, “Potatoes have more chromosomes than humans.”, “You burn more calories sleeping than you do watching television.”, “Animals […]