我正在用Java构建一个基本的战舰式游戏,并使用嵌套列表来表示游戏网格.但是,当我试图放下一艘船时,我一直得到IndexOutOfBoundsException.
游戏板具有如下构造函数
public Board(){
theSea = new ArrayList<ArrayList<ShipInterface>>(10);
for(int i = 0; i < theSea.size(); i++){
theSea.set(i, new ArrayList<ShipInterface>(10));
}
}
Run Code Online (Sandbox Code Playgroud)
放置船舶的方法如下:
public void placeShip(ShipInterface ship, Position position, boolean isVertical) throws InvalidPositionException, ShipOverlapException{
for(int i=0; i<ship.getSize(); i++){
theSea.get((position.getX()-1) + i).set(position.getY()-1, ship);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到了错误 theSea.get((position.getX()-1) + i).set(position.getY()-1, ship);
我是初学者,如果我错过了一些明显的代码,我很抱歉!
我有一个arraylist arraylists有字符串[] s即,
ArrayList<ArrayList> arraymain = new ArrayList<>();
ArrayList<String[]> arraysub = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
现在,将元素添加到arraysub,
arraysub.add(new String[]{"vary","unite"});
arraysub.add(new String[]{"new","old"});
Run Code Online (Sandbox Code Playgroud)
我试过了:
arraymain.get(0).get(0)[1];
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:预期的数组类型; 找到'java.lang.Object'我现在如何检索数据(存储在字符串[]中的字符串)?
我正在尝试制作一个玩家只能在他们周围的小半径中看到的游戏.我试图通过覆盖具有1X1黑色像素的500X500显示器来实现这一点,我可以将其设置为活动或非活动状态.问题是使用standered for循环来添加它们会花费大量的时间来启动程序并且它会降低整个程序的速度.任何方案?
pix对象有两个参数(int x,int y)
码
public ArrayList<Pix> pixs= new ArrayList<>();
for(int i = 0; i<=500; i++)
{
for(int ii = 0; ii<=500; ii++)
{
pixs.add(new Pix(ii,i));
}
}
Run Code Online (Sandbox Code Playgroud) 按照这个
HashSet的基础数据结构是哈希表.
但我还读到HashSet在内部使用HashMap来避免重复值,而重复值又在内部使用了桶数组和LinkedList(在Java 8中由树替换)
那么说HashSet使用HashTable作为数据结构和HashMap作为集合是正确的吗?
TreeSet实现SortedSet接口,因此不允许重复值.
这是否意味着TreeSet不在内部使用HashMap来使用HashSet来避免重复值?LinkedHashSet是否在内部使用HashMap?
按照这个
建议不要使用内存点视图数组.
为什么?通过我之前阅读的内容 -
由于无法为原始数据类型创建ArrayList,因此ArrayList的成员始终引用不同内存位置的对象(有关详细信息,请参阅此处).因此,在ArrayList中,实际对象永远不会存储在连续的位置.实际对象的引用存储在连续的位置.在数组中,它取决于数组是基本类型还是对象类型.在原始类型的情况下,实际值是连续的位置,但是在对象的情况下,分配类似于ArrayList.
我有一个枚举的ArrayList,但我需要确保只有一个特定的枚举(或任何它的名字).我应该创建一个扩展ArrayList的新类并检查输入吗?我已经看到,也许最好不要(你能用Java扩展ArrayList吗?)但是我看不到更好的方法.
我的枚举
enum myEnum{
SPECIAL,
OTHER1,
OTHER2,//etc
}
Run Code Online (Sandbox Code Playgroud)
使用ArrayList
List<myEnum> list = new ArrayList<>();
list.add(myEnum.SPECIAL);
list.add(myEnum.OTHER1);
list.add(myEnum.SPECIAL);//OH NO!!!
Run Code Online (Sandbox Code Playgroud)
没有ArrayList
class myList extends ArrayList<myEnum> {
@Override
public boolean addAll(int i, Collection<? extends myEnum> clctn) {
if(check()){
super.addAll(i, clctn);
}else{
//do something or throw error;
}
}
@Override
public boolean addAll(Collection<? extends myEnum> clctn) {
if(check()){
super.addAll(clctn);
}else{
//do something or throw error;
}
}
@Override
public void add(int i, myEnum e) {
if(check()){
super.add(i, e);
}else{
//do something or …Run Code Online (Sandbox Code Playgroud) 我有两个列表可能如下所示:
A: {1 3 1 2}
B: {1 3}
Run Code Online (Sandbox Code Playgroud)
我想B从列表中删除列表元素,A最后得到:
A: {1 2}
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用,A.removeAll(B)我将只得到值2,因为值1的所有重复实例将从列表中删除A.到目前为止,我的解决方案是对所有元素使用迭代器A,然后使用所有元素,B如果我找到类似的值,则从两个列表中删除它,直到列表B为空.
有没有更好的方法呢?
每当我尝试从List使用list.removeIf(condition)中删除一个元素时,它会抛出UnsupportedOperationException:
public class Test
{
public static void main(final String[] args)
{
String[] stringArray = new String[]{"A","B","C","D"};
List<String> stringList = Arrays.asList(stringArray);
stringList.forEach(System.out::println);
stringList.removeIf((String string) -> string.equals("B"));
stringList.forEach(System.out::println);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不起作用?
I am trying to retrieve common 'keys' from two hashMaps and put the result in a ArrayList. To do that, I have come up with following logic.
List<String> commonList = new ArrayList<String>();
for(String key : mapA.keySet()) {
if(mapB.get(key) !=null ) {
if(mapA.get(key).equals(mapB.get(key))) {
commonList.add(key);
}
}
}
Run Code Online (Sandbox Code Playgroud)
This is the data I added in the maps: mapA & mapB:
mapA.put("abc_ap_bank_accounts,XYZ","6372,43272.6648842593");
mapA.put("abc_ap_bank_accounts_secured_attributes,XYZ","6372,43272.6648829051");
mapA.put("abc_ap_checks,XYZ","56207,43272.676245");
mapA.put("abc_ap_holds,XYZ","9523,43272.6735710995");
mapA.put("abc_ap_holds_dh,XYZ","14,43272.6735710995");
mapA.put("abc_ap_invoice_distributions,XYZ","1573699,43272.6735710995");
mapA.put("abc_ap_invoice_distributions_dh,XYZ","9,43272.6735710995");
mapA.put("abc_ap_invoices,XYZ","141096,43272.6735710995");
mapA.put("abc_ap_invoices_dh,XYZ","47,43272.6735710995");
mapA.put("abc_ap_payment_history,XYZ","454441,43272.6763922106");
mapA.put("abc_ap_payment_methods,XYZ","41,43193.0547537269");
mapA.put("abc_ap_payment_schedules,XYZ","141099,43272.6735710995");
mapA.put("abc_ap_payment_schedules_dh,XYZ","47,43272.6735710995");
mapA.put("abc_ap_terms,XYZ","73,43193.0547620718");
mapA.put("abc_ar_cash_receipts,XYZ","198815,43272.6634247337");
mapA.put("abc_ar_collectors,XYZ","8,43192.4939946643");
mapA.put("abc_ar_customer_contacts,XYZ","4978,43272.6613442824");
mapA.put("abc_ar_customer_site_uses,XYZ","71313,43272.6617516204");
mapA.put("abc_ar_customer_sites,XYZ","38740,43272.6617516204");
mapA.put("abc_ar_customers,XYZ","12521,43272.6617516204"); …Run Code Online (Sandbox Code Playgroud) 如何从java输出中删除'['']'.我需要在没有方括号的情况下打印它们,因为这是我的要求.
代码已在下面附上.
package Datas;
import java.util.ArrayList;
import java.util.Collections;
public class SortingLists {
public static void main(String[] args) {
ArrayList<String> animals = new ArrayList<String>();
animals.add("tiger");
animals.add("cat");
animals.add("snake");
animals.add("dog");
//Sort by alphabet
Collections.sort(animals);
System.out.println(animals);
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(3);
nums.add(36);
nums.add(73);
nums.add(40);
nums.add(1);
//Sort number
Collections.sort(nums);
System.out.println(nums);
}
Run Code Online (Sandbox Code Playgroud)
}
假设我在同一控制器中有一个可观察的列表和一个按钮
private ObservableList<NameItem> _selectedList = _database.getONameList();
@FXML
private Button nextButton;
Run Code Online (Sandbox Code Playgroud)
我如何做到只有在ObservableList大于0时才启用按钮,否则将其禁用?我可以使用绑定属性来设置它吗?
我尝试使用此:https : //stackoverflow.com/a/38216335/5709876
但是它对我没有用..
arraylist ×10
java ×9
collections ×2
hashmap ×2
android ×1
arrays ×1
bluej ×1
for-loop ×1
javafx ×1
list ×1
nested ×1
observable ×1
optimization ×1
sorting ×1
string ×1