Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将.indexOf()与ArrayList一起使用<;字符串>;_Java_Arrays - Fatal编程技术网

Java 将.indexOf()与ArrayList一起使用<;字符串>;

Java 将.indexOf()与ArrayList一起使用<;字符串>;,java,arrays,Java,Arrays,我试图用Java创建一个战舰游戏。实际的游戏是有效的,但我在玩的过程中遇到了一个令人惊讶的问题。下面的代码从49个值的网格中生成一个随机位置,用户必须根据这些值进行猜测。作为一个调试工具,我想我会打印出ArrayList的数字值的索引,但是我得到了-1(发现意味着找不到值)。考虑到我的数学是正确的,为什么它不返回一个数字呢 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRe

我试图用Java创建一个战舰游戏。实际的游戏是有效的,但我在玩的过程中遇到了一个令人惊讶的问题。下面的代码从49个值的网格中生成一个随机位置,用户必须根据这些值进行猜测。作为一个调试工具,我想我会打印出ArrayList的数字值的索引,但是我得到了-1(发现意味着找不到值)。考虑到我的数学是正确的,为什么它不返回一个数字呢

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class GameHelper 
{
String alphabet = "abcdefg";
private int gridLength=7;
private int gridSize=49;
private int [] grid = new int [gridSize];
private int comCount=0;

public String getUserInput(String prompt)
{
    String inputLine=null;
    System.out.println(prompt);

    try
    {
        BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
        inputLine=is.readLine();
        if (inputLine.equals(null))
        {
            return null;
        }
    }

    catch(IOException e)
    {
        System.out.println("Buffered Reader Failure");
    }
    return inputLine;
}


public ArrayList <String> placeDotCom (int size)
{
    ArrayList <String> alphaCells = new ArrayList <String>();

    String temp=null;
    int [] coords=new int[size];
    int attempts=0;
    boolean success=false;
    int location=0;

    comCount++;
    int incr=1;
    if((comCount%2)==1)
    {
        incr=gridLength;
    }

    while (!success & attempts++ < 200)
    {
        location = (int)(Math.random()*gridSize);
        int x=0;
        success=true;
        while (success && x < size)
        {
            if (grid[location] ==0)
            {
                coords[x++]=location;
                location+=incr;
                if(location >= gridSize)
                {
                    success = false;
                }
                if(x>0 && (location % gridLength ==0))
                {
                    success = false;
                }
            }
                else
                {
                    System.out.println("used" + location);
                    success=false;
                }
            }
        }


        int x=0;
        int row=0;
        int column=0;

        while(x < size)
        {
            grid[coords[x]] = 1;
            row = (int)(coords[x]/gridLength);
            column = coords[x] % gridLength;
            temp = String.valueOf(alphabet.charAt(column));

            alphaCells.add(temp.concat(Integer.toString(row)));
            x++;
        }
        System.out.println("coord "+x+" = "+alphaCells.get(x-1));
        return alphaCells;
    }

} 
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
公共类游戏助手
{
字符串字母表=“abcdefg”;
私有int gridLength=7;
私有int gridSize=49;
private int[]grid=new int[gridSize];
私有int comCount=0;
公共字符串getUserInput(字符串提示)
{
字符串inputLine=null;
System.out.println(提示);
尝试
{
BufferedReader is=新的BufferedReader(新的InputStreamReader(System.in));
inputLine=is.readLine();
if(inputLine.equals(null))
{
返回null;
}
}
捕获(IOE异常)
{
System.out.println(“缓冲读卡器故障”);
}
返回输入线;
}
公共ArrayList placeDotCom(整数大小)
{
ArrayList alphaCells=新的ArrayList();
字符串temp=null;
int[]坐标=新的int[size];
int=0;
布尔成功=假;
int位置=0;
comCount++;
int incr=1;
如果((comCount%2)==1)
{
增量=网格长度;
}
而(!成功和尝试+++<200)
{
位置=(int)(Math.random()*gridSize);
int x=0;
成功=真实;
while(success&&x=网格大小)
{
成功=错误;
}
如果(x>0&&(位置%gridLength==0))
{
成功=错误;
}
}
其他的
{
System.out.println(“已使用”+位置);
成功=错误;
}
}
}
int x=0;
int行=0;
int列=0;
while(x
我的尝试呼叫:

for (DotCom dcset : dc)
{
ArrayList <String> newLocation = helper.placeDotCom(3);
dcset.setLocationCells(newLocation);
System.out.println(newLocation.indexOf(1));
}
for(DotCom dcset:dc)
{
ArrayList newLocation=helper.placeDotCom(3);
dcset.setLocationCells(新位置);
System.out.println(newLocation.indexOf(1));
}
我想这是唯一相关的代码。如果你想让我发布更多,请告诉我。这是我第一次发帖,我不确定我问的问题是否正确。这太模糊了吗?

来自

返回此列表中指定元素第一次出现的索引,如果此列表不包含该元素,则返回
-1
。更正式地说,返回最低的索引
i
,这样
对象就等于(o,get(i))
,或者如果没有这样的索引,则返回
-1

注意,该方法需要一个
对象
作为参数(这有历史原因,它起源于黑暗时代,Java中不存在泛型,所有容器都使用
对象

现在,您声明了
arraylistnewlocation
并在此对象上调用
.indexOf(1)
。传递的
int
-值进入。由于您的
newLocation
-列表仅包含
字符串
s,因此无法在列表中找到传递的
整数。因此,它返回一个
-1



关于代码的备注:正如我所写,
inputLine.equals(null)
将返回
false
(如果
inputLine
不是
null
),或者抛出
NullPointerException
(如果
inputLine
null
)。这是因为,如果程序试图访问
null
-引用上的属性或方法,将抛出
NullPointerException
。只需写入
inputLine==null
(在本例中,
=
是正确的)。另一种解决方案是:您可以使用检查
inputLine
是否为
null

if(inputLine.equals(null))
”—这将返回
false
或抛出
null点异常
,但您想做什么<列表上的code>indexOf()
需要一个对象作为参数,这是您要寻找的,一个整数(1)还是您想调用
get(1)
还是…?为什么您希望
ArrayList上的
indexOf(1)
能够找到任何东西?
整数
值(
1
)永远不等于
字符串
值。啊,我的错误。我认为它的工作原理类似于.substring(使用1作为参数将返回占据ArrayList第一部分的对象)。更好的可读性。@Szychan感谢您的评论!我在答复中采纳了你的建议。