我有一系列的java.awt.Rectangles.对于那些不熟悉这门课程的人来说,重要的信息是它们提供了一种.intersects(Rectangle b)功能.
我想编写一个函数来获取这个Rectangles 数组,并将其分解为连接的矩形组.
比方说,例如,这些是我的矩形(构造函数采用的参数x,y,width,height):
Rectangle[] rects = new Rectangle[]
{
new Rectangle(0, 0, 4, 2), //A
new Rectangle(1, 1, 2, 4), //B
new Rectangle(0, 4, 8, 2), //C
new Rectangle(6, 0, 2, 2) //D
}
Run Code Online (Sandbox Code Playgroud)
快速绘图显示A相交B和B相交C.D不相交.一个繁琐的ascii艺术作品也完成了这项工作:
????????? ?????
?A????? ? ? D ?
????????? ?????
? B ?
?????????????????
? ????? C ?
?????????????????
Run Code Online (Sandbox Code Playgroud)
因此,我的函数的输出应该是:
new Rectangle[][]{
new Rectangle[] {A,B,C},
new Rectangle[] {D}
} …Run Code Online (Sandbox Code Playgroud) 为什么我会得到不同的行为:
Collection col2 = new ArrayList(col);
Collection col2 = new ArrayList();
col2.addAll(col)
我正在与观众合作,代码很复杂,我试图解释问题的"根".另一个有趣的事实是下一个......
//IF i use this code i have the correct behavior in my app:
public void updateCollection(Collection<Object> col) {
this.objectCollection.clear();
this.objectCollection.addAll(col);
}
//IF i use this code i have unexpected behavior in my app:
public void updateCollection(Collection<Object> col) {
this.objectCollection=new ArrayList(col);
}
Run Code Online (Sandbox Code Playgroud) 有点奇怪的要求.
public class DummyClass{
public static final DummyClass var1;
public static final DummyClass var2;
public static final DummyClass var3;
.
.
.
public static final DummyClass var100;
}
Run Code Online (Sandbox Code Playgroud)
现在,从这个类的外部我们可以将这个var汇集到一个数组或列表中,以便我可以迭代它们吗?就像我做的那样
List<DummyClass> dummyList = *some op*; //I want value of some op.
Run Code Online (Sandbox Code Playgroud)
我应该能够访问var1 ... var100
我在将这个ArrayList声明为Java中的实例变量时遇到错误.
private ArrayList<char> correctGuesses = new ArrayList<char>();
Run Code Online (Sandbox Code Playgroud)
错误:
Syntax error on token char, Dimension expected after this token
Run Code Online (Sandbox Code Playgroud)
我可以不使用char类型创建ArrayLists吗?
我有一个叫做的课
class Student {
String name;
String age;
}
Run Code Online (Sandbox Code Playgroud)
我有一个返回List对象的方法
public List<Student> getList(){
List<Student> li =new ArrayList();
....
li.add(new Student('aaa','12'));
...
return li;
}
Run Code Online (Sandbox Code Playgroud)
我需要将该列表转换为JSONArray
[{"name":"sam","age":"12"},{"name":"sri","age":"5"}]
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我这个吗?先谢谢你...
我尝试了下面的代码,但它不起作用.我是否必须将循环复制ArrayList到数组中?
int[]intArray = (int[]) integerArrayList.toArray();
Run Code Online (Sandbox Code Playgroud) 通常,如果我们想要初始化通用的非原始ArrayList,我们就这样做
ArrayList<?> arrayList = new ArrayList<MyClass.class>();
Run Code Online (Sandbox Code Playgroud)
但是无论我传递哪个类对象,我想做类似的事情,即
private void getModel(Class responseType){
//Something similar, because this does not work..
ArrayList<?> arrayList = new ArrayList<responseType>();
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
我正在尝试规范化一串以逗号分隔的数字和一个范围.展示:
数组:
$array = ["1","2","5-10","15-20"];
Run Code Online (Sandbox Code Playgroud)
应成为:
$array = [1,2,5,6,7,8,9,10,15,16,17,18,19,20];
Run Code Online (Sandbox Code Playgroud)
我正在研究的算法是:
//get the array values with a range in it :
$rangeArray = preg_grep('[-]',$array);
Run Code Online (Sandbox Code Playgroud)
这将包含["5-10", "16-20"];Then:
foreach($rangeArray as $index=>$value){
$rangeVal = explode('-',$value);
$convertedArray = range($rangeVal[0],$rangeVal[1]);
}
Run Code Online (Sandbox Code Playgroud)
转换后的数组现在包含["5","6","7","8","9","10"] ;
我现在面临的问题是,如何在原始数组中弹出值"5-10",并在$ convertedArray中插入值,以便我将获得值:
$ array = ["1","2","5","6","7","8","9","10","16-20"];
那么,如何将数组插入数组?或者有更清洁的方法来解决这类问题吗?将数字和范围值的数组转换为正确排序的数字数组?
Iterable<Board> theNeighbors = new ArrayList<Board>();
Run Code Online (Sandbox Code Playgroud)
这是我对ArrayListtheNeighbors的初始化,它使用了interafce Iterable进行声明.但是,当我使用方法add()来构建我刚刚构建的变量时,编译器会发出警报
Board.java:78:错误:找不到符号theNeighbors.add(nb); ^
符号:方法添加(Board)
位置:变量类型Iterable的邻居
是什么让它发生?在我使用的另一种情况
List<Board> theNeighbors = new ArrayList<Board>();
Run Code Online (Sandbox Code Playgroud)
该add()方法效果很好.您为声明选择的界面是否应始终具有您稍后要调用的方法?
如何使用Optionals将此函数重写为更多Java 8?或者我应该保持原样?
public void setMemory(ArrayList<Integer> memory) {
if (memory == null)
throw new IllegalArgumentException("ERROR: memory object can't be null.");
if (memory.contains(null))
throw new IllegalArgumentException("ERROR: memory object can't contain null value.");
this.memory = memory;
}
Run Code Online (Sandbox Code Playgroud)