将元素添加到动态二维arraylist [英] Add elements to a dynamic two dimensional arraylist

查看:80
本文介绍了将元素添加到动态二维arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查数组的元素,如果它小于200或大于800,它将添加到数组1,其他将添加到数组2,数组1和数组2将在一个二维的arraylist,无论如何它看起来像这样:



int [] arr = {171,100,809,70,365,239,37,821,791,452,349};



结果:



结果[0] = {171,100,809,70,37,821}

结果[1] = { 365,239,791,452,349}





这是我的代码:



  import  java.util.ArrayList; 

public class twoDimen {

public static void main( String [] args){
int [] arr = { 171 100 809 70 365 239 37 821 791 452 ,< span class =code-digit> 349 };
ArrayList< ArrayList< Integer>> result = new ArrayList< ArrayList< Integer>>();

for int i = 0 ; i< = arr.length; i ++){
if (arr [i]< 200 || arr [i]> 800 ){
result.get( 0 )添加(ARR [I])。
}
else
result.get( 1 )。 (ARR [I]);
}

System.out.println(result.get( 0 ));
System.out.println(result.get( 1 ));
}
}





我执行它之前没有显示任何错误,但是有错误执行时:

线程main中的异常java.lang.IndexOutOfBoundsException:索引:0,大小:0

at java.util.ArrayList.rangeCheck(Unknown Source )

在java.util.ArrayList.get(未知来源)

在twoDimen.main(twoDimen.java:11)​​



我该如何解决?我可以使用动态二维数组而不是arraylist吗?



感谢任何人的帮助。

解决方案

< blockquote>在你将任何数组添加到 result 之前,你正在做 result.get(0)



改变这个;

 ArrayList< arraylist>< integer>> result =  new  ArrayList< arraylist>< integer>>(); 
< / integer>< / arraylist>< / integer>< / arraylist>





到此;

 ArrayList< arraylist>< integer>> result =  new  ArrayList< arraylist>< integer>>(); 
result.add( new ArrayList< integer>());
result.add( new ArrayList< integer>());
< / integer>< / integer>< / integer>< / arraylist>< / integer>< / arraylist>







希望这会有所帮助,

Fredrik


Hi, I want to check an array''s elements, if it smaller than 200 or lager than 800, it will add to array 1, and others will be added to array 2, array 1 and array 2 will be in a two dimensional arraylist, anyway it looks like this:

int[] arr = {171,100,809,70,365,239,37,821,791,452,349};

result:

result[0] = {171,100,809,70,37,821}
result[1] = {365,239,791,452,349}


And here is my code:

import java.util.ArrayList;

public class twoDimen {

	public static void main (String[] args){
		int[] arr = {171,100,809,70,365,239,37,821,791,452,349};
		ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
		
		for (int i = 0; i <= arr.length; i++){
			if (arr[i] < 200 || arr[i] > 800){
				result.get(0).add(arr[i]);
			}
			else
				result.get(1).add(arr[i]);		
		}
		
		System.out.println(result.get(0));
		System.out.println(result.get(1));
	}	
}



it does not show any errors before I execute it, but there''s errors when executing it:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at twoDimen.main(twoDimen.java:11)

How can I fix it? And can I use dynamic two dimensional arrays instead of arraylist?

Thanks anyone''s help.

解决方案

You''re doing result.get(0) before you''ve added any arrays to result.

Change this;

ArrayList<arraylist><integer>> result = new ArrayList<arraylist><integer>>();
</integer></arraylist></integer></arraylist>



To this;

ArrayList<arraylist><integer>> result = new ArrayList<arraylist><integer>>();
result.add(new ArrayList<integer>());
result.add(new ArrayList<integer>());
</integer></integer></integer></arraylist></integer></arraylist>




Hope this helps,
Fredrik


这篇关于将元素添加到动态二维arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆