我有一个String数组,如,
String[] abc= new String[]{};
Run Code Online (Sandbox Code Playgroud)
我的名单有一些价值.我迭代列表并将每个列表元素添加到字符串数组.
for(int i=0; i<errList.size(); i++)
{
abc[i] = errList.get(i).getSrceCd();
}
Run Code Online (Sandbox Code Playgroud)
errList.size()有6个值.但是当for循环执行时,我得到了java.lang.ArrayIndexOutOfBoundsException.有什么投入?
我正在处理从文件中读取的作业,并检测哪个字母使用最多,哪个字母使用最少.这是我得到的输出.
Would you like to input a directory for a .dat file? Input Yes or No
Yes
Please input file location
G:\test.dat
Exception in thread "main" java.lang.NullPointerException
at Histogram.loadAndAnalyzeFile(Histogram.java:62)
at lab21c.main(newlab21c.java:15)
Process completed.
Run Code Online (Sandbox Code Playgroud)
这是我的Histrogram文件
//© A+ Computer Science - www.apluscompsci.com
//Name -
//Date -
//Lab -
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.System.*;
public class Histogram
{
private ArrayList<Integer> count;
int counter = 0;
private String[] Letters = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
private String fileName;
ArrayList<String> storageStr; …Run Code Online (Sandbox Code Playgroud) 我正在尝试对我的HasMap ArrayList进行排序,因此我的列表视图按值排序,但我没有得到它.基本上我有几个键,其中一个是"类型",其中包含值"1", "4", "3",....
我想通过这个键"类型"来命令列表,但我得到的"1", "11", "2"不是"1", "2", "11"......
我正在尝试使用此代码对其进行排序:
Collections.sort(myList, new Comparator<HashMap<String, String>>() {
public int compare(HashMap<String,
String> mapping1,HashMap<String, String> mapping2) {
return mapping1.get("type").compareTo(mapping2.get("type"));
}
});
Run Code Online (Sandbox Code Playgroud) 基本上,我正在尝试编写一个程序,将数字从基数2转换为基数10.我尝试做的是将本网站上列出的"加倍方法"下的过程转换为for循环,但由于某种原因,数字我变得很大.
基本公式是(2*previousTotal)+(保存用户输入的二进制数的ArrayList的currentDigit)= previousTotal.
所以对于二进制的1011001,数学将是:
然而,控制台打印出6185作为结果.我想它可能与我使用字符的ArrayList有关,但是charWhole.size()返回7,这是用户的二进制数中有多少位数.我一做charsWhole.get(w); 然而,我开始得到像49这样的大数字.我真的很感激一些帮助!
我写了这个循环,并根据我在整个代码中放置的一些打印语句和我的变量addThis似乎是问题所在.控制台打印出最终总数为6185,而基数10中的1011001实际为89.
public static void backto2(){
System.out.println("What base are you coming from?");
Scanner backToB10 = new Scanner(System.in);
int bringMeBack = backToB10.nextInt();
//whole
System.out.println("Please enter the whole number part of your number.");
Scanner eachDigit = …Run Code Online (Sandbox Code Playgroud) 这可能是一个愚蠢的问题,但令我感到沮丧.我在循环期间在循环外添加到我的数组列表中的对象,所有数组列表都被数组中的最后一个元素覆盖.它就是这样的.
while(fin.hasNextLine())
{
String line = fin.nextLine();
String[] user = line.split(",");
r.add(new User(user[0], user[1]));
System.out.println(r.get(count).getName());
count++;
}
Run Code Online (Sandbox Code Playgroud)
在循环期间,这给了我类似的输出(USER1,USER2,USER3等).但是,在循环之后我现在有一个这样的输出(USER500,USER500,USER500).
while(fin.hasNextLine())
{
String line = fin.nextLine();
String[] user = line.split(",");
r.add(new User(user[0], user[1]));
System.out.println(r.get(count).getName());
count++;
}
for (int i =0; i < r.size(); i++)
{
System.out.println(r.get(i).getName());
}
Run Code Online (Sandbox Code Playgroud)
我设法验证这是我遇到问题的类,只有一个方法使用我注释掉的这个类中的数组列表.
我有以下for循环,它查看结果的字符串ArrayList,字符串中的每个项目都用"::"分隔:
ArrayList<String> resultsArray = MyClass.results;
Integer numPoints = resultsArray.size();
for (int i =0;i<numPoints;i++){
String[] pointDetails = resultsArray.get(i).split("::");
String pointName = pointDetails[0];
String pointDescription = pointDetails[1];
String coordinates = pointDetails[2];
//Turn coordinates into geopoints
String coord[] = coords.split(",");
Integer lng= (int) (Double.valueOf(coord[0]) * 1000000);
Integer lat = (int)(Double.valueOf(coord[1])*1000000);
GeoPoint gPoint = new GeoPoint(lng,lat);
arrayPointName = new ArrayList <String>();
arrayPointDescription = new ArrayList <String>();
arrayPointCoords=new ArrayList<GeoPoint>();
arrayPointName.add(pointName);
arrayPointDescription.add(pointDescription);
arrayPointCoords.add(gPoint);
}
Run Code Online (Sandbox Code Playgroud)
我知道我在初始字符串ArrayList中有20个点,并打印出它的大小来检查这个.但是,当我打印出新的arraylists时,例如arrayPointName,它们只包含一个点.有什么想法吗?
我定义一个ArrayList如下;
static ArrayList<Double[]> aryList1 = new ArrayList<Double[]>();
Run Code Online (Sandbox Code Playgroud)
我希望将其添加到arraylist中
double[] a = new double[]{0.1,0.2};
arylist1.add(a);
Run Code Online (Sandbox Code Playgroud)
但它显示错误
arylist1.add(a)
Run Code Online (Sandbox Code Playgroud)
... add不能应用于double ArrayList.
这会让我的大脑受到伤害......
我想向一个arraylist添加多个变量,如日期和时间,并将该arraylist添加到另一个包含名称和年龄等不同变量的arraylist.
示例:ArrayList包含多个帐户.每个帐户都有ID,名称,余额和交易历史记录.每笔交易都有id,日期,时间和交易金额.
因此,帐户ArrayList将包含:(int | String | double | ArrayList),
并且事务ArrayList将包含:(int | Date | Time | double).
如果您能帮助我了解如何做到这一点,或者提供更好的解决方案,我将不胜感激.
编辑:
我无法真正显示我的所有代码,因为我正在使用模型视图控制器访问模型和4个视图.我可以尝试描述它的功能.
点击"创建帐户"按钮:
点击"存款"按钮:
实际上我想对Array List of objects进行排序.我为此目的使用Comparable接口.它完全正常工作但问题是,当我进行排序时,就是给出了这两个问题.
所有拥有首字母大写字母的名字都汇集在一起,所有名字第一个字母小的名字都在底部聚集在一起.
所有排序后的大写字母单词都汇集在一起,之后所有的小写字母一起出现在底部.
这是我的豆子
MyShares.java
public class Myshares implements Comparable<Myshares> {
int id, parent;
String name, path, type, shared_with, shared_or_not, upload_request;
public int getParent() {
return parent;
}
public void setParent(int parent) {
this.parent = parent;
}
public String getUpload_request() {
return upload_request;
}
public void setUpload_request(String upload_request) {
this.upload_request = upload_request;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public …Run Code Online (Sandbox Code Playgroud) 是否有一种简单的方法来创建一个int[]或等同Integer[]于ArrayList<Integer>?
当我尝试时(Integer[])list.getArray(),我得到这个堆栈跟踪:
线程"main"中的异常java.lang.ClassCastException:[Ljava.lang.Object; 无法转换为[Ljava.lang.Integer;
我能想到的另一种方式是:
int[] array = new int[list.size()];
for (int i = 0; i < array.size; i++){
array[i] = list.get(i);
//or I could do list.remove(0), is there any difference?
}
return array;
Run Code Online (Sandbox Code Playgroud)
但那种方式似乎非常缓慢.