Java 如何测试ArrayList中的某个项目<;字符串>;在某个地方

Java 如何测试ArrayList中的某个项目<;字符串>;在某个地方,java,swing,if-statement,arraylist,Java,Swing,If Statement,Arraylist,现在我正在做一个if语句,它看起来像这样: if(Game.nflteams.subList(0, 1).equals("Arizona Cardinals")) // do something like draw their logo to a made JFrame 每次Game.nflteams.subList(0,1)等于“亚利桑那红衣主教队”,或者我到目前为止与之合作的3支球队中的一支,屏幕上都不会出现任何东西。 我所有的课程都在下面: 我的主要班级: public clas

现在我正在做一个if语句,它看起来像这样:

if(Game.nflteams.subList(0, 1).equals("Arizona Cardinals"))
    // do something like draw their logo to a made JFrame
每次
Game.nflteams.subList(0,1)
等于
“亚利桑那红衣主教队”
,或者我到目前为止与之合作的3支球队中的一支,屏幕上都不会出现任何东西。
我所有的课程都在下面:
我的主要班级:

public class Football {

    public static void main(String[] args) {
        Game game = new Game();
        game.game();
        GameII gamei = new GameII("Football Sim", 1000, 500);
        gamei.start();
    }

}
我的游戏课:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Game {

    private static ArrayList<String> teams = new ArrayList<String>();

    private Random random = new Random();

    public void game() {
        teams.add("New England Patriots (0 - 1)");
        teams.add("Dallas Cowboys (0 - 1)");
        teams.add("Oakland Raiders (1 - 0)");
        teams.add("Philadelphia Eagles (0 - 1)");
        teams.add("New York Giants (0 - 1)");
        teams.add("Seattle Seahawks (1 - 0)");
        teams.add("Pittsburgh Steelers (1 - 0)");
        teams.add("Green Bay Packers (1 - 0)");
        teams.add("Denver Broncos (0 - 1)");
        teams.add("San Fransisco 49ers (0 - 1)");
        teams.add("Chicago Bears (1 - 0)");
        teams.add("Minnesota Vikings (0 - 1)");
        teams.add("Carolina Panthers (1 - 0)");
        teams.add("Cleveland Browns (0 - 1)");
        teams.add("Los Angeles Rams (1 - 0)");
        teams.add("Kansas City Cheifs (0 - 1)");
        teams.add("Washington Redskins (1 - 0)");
        teams.add("Atlanta Falcons (1 - 0)");
        teams.add("Baltimore Ravens (1 - 0)");
        teams.add("Houston Texans (0 - 1)");
        teams.add("Detroit Lions (1 - 0)");
        teams.add("New York Jets (0 - 1)");
        teams.add("Los Angeles Chargers (0 - 1)");
        teams.add("Arizona Cardinals (0 - 1)");
        teams.add("Buffalo Bills (1 - 0)");
        teams.add("New Orleans Saints (1 - 0)");
        teams.add("Miami Dolphins (0 - 1)");
        teams.add("Jacksonville Jaguars (1 - 0)");
        teams.add("Cincinatti Bengals (0 - 1)");
        teams.add("Tampa Bay Buccaneers (1 - 0)");
        teams.add("Indianapolis Colts (0 - 1)");
        teams.add("Tennessee Titans (1 - 0)");
        Collections.shuffle(teams);
        int t2touch = (random.nextInt(5) + 1) * 7;
        int t2fielgoal = (random.nextInt(5) + 1) * 3;
        int t2score = t2touch + t2fielgoal;
        int t1touch = random.nextInt(5) * 7;
        int t1fielgoal = random.nextInt(5) * 3;
        int t1score = t1touch + t1fielgoal;
        System.out.println(teams.subList(0, 1) + " (away) " + " vs " + teams.subList(1, 2) + " (home)");
        if(t1score > t2score) {
            System.out.println("The " + teams.subList(0, 1) + " win " 
                    + t1score + " (" + t1touch / 7 + " touchdowns, " + t1fielgoal / 3 + " field goals)" + 
                    " to "
                    + t2score + " (" + t2touch / 7 + " touchdowns, " + t2fielgoal / 3 + " field goals)" + "!");
        } else if(t2score > t1score) {
            System.out.println("The " + teams.subList(1, 2) + " win " 
                    + t2score + " (" + t2touch / 7 + " touchdowns, " + t2fielgoal / 3 + " field goals)" + 
                    " to "
                    + t1score + " (" + t1touch / 7 + " touchdowns, " + t1fielgoal / 3 + " field goals)" + "!");
        } else {
            System.out.println("It's a tie. "
                    + teams.subList(0, 1) + " " + t1score + " (" + t1touch / 7 + " touchdowns, " + t1fielgoal / 3 + " field goals" + 
                    ") to "
                    + teams.subList(1, 2) + " " + t2score + " (" + t2touch / 7 + " touchdowns, " + t2fielgoal / 3 + " field goals");
        }
    }

    public static ArrayList<String> getTeams() {
        return teams;
    }

}
我的资产类别:

import java.awt.image.BufferedImage;

public class Assets {

    private static final int width = 134, height = 100;

    public static BufferedImage cardinals, jaguars, patriots, saints, browns, broncos;

    public static void init(){
        SpriteSheet sheet = new SpriteSheet(ImageLoader.loadImage("/textures/every nfl team.jpg"));

        cardinals = sheet.crop(0, 0, width, height);
        jaguars = sheet.crop(width, 0, width, height);
        patriots = sheet.crop(width * 2, 0, width, height);
        saints = sheet.crop(width * 4, 0, width, height);
        browns = sheet.crop(width * 5, 0, width, height);
        broncos = sheet.crop(width * 6, 0, width, height);
    }
}
我的SpriteSheet类:

import java.awt.image.BufferedImage;

public class SpriteSheet {

    private BufferedImage sheet;

    public SpriteSheet(BufferedImage sheet){
        this.sheet = sheet;
    }

    public BufferedImage crop(int x, int y, int width, int height){
        return sheet.getSubimage(x, y, width, height);
    }

}
我的ImageLoader类:

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageLoader {

    public static BufferedImage loadImage(String path){
        try {
            return ImageIO.read(ImageLoader.class.getResource(path));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        return null;
    }

}
和我的显示类:

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Display {

    private JFrame frame;
    private Canvas canvas;

    private String title;
    private int width, height;

    public Display(String title, int width, int height){
        this.title = title;
        this.width = width;
        this.height = height;

        createDisplay();
    }

    private void createDisplay(){
        frame = new JFrame(title);
        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        canvas = new Canvas();
        canvas.setPreferredSize(new Dimension(width, height));
        canvas.setMaximumSize(new Dimension(width, height));
        canvas.setMinimumSize(new Dimension(width, height));

        frame.add(canvas);
        frame.pack();
    }

    public Canvas getCanvas() {
        return canvas;
    }

}
这些都是我的课程,应该足以重现问题。
只是说:
理论上/逻辑上,我的公式应该有效。
仔细想想,我的步骤完全合乎逻辑:
1) 制作一个字符串数组列表,并将所有NFL球队的名称添加到该列表中
2) 洗牌数组列表
3) 随机选择两个“团队”进行“比赛”
4) 测试是否选择了特定的团队

5) 如果这是真的,请将该团队的徽标绘制到先前制作的JFrame上,这需要大量代码。但我认为您应该使用
包含
而不是
等于

if(Game.nflteams.subList(0, 1).contains("Arizona Cardinals"))

这是很多代码。但我认为您应该使用
包含
而不是
等于

if(Game.nflteams.subList(0, 1).contains("Arizona Cardinals"))

.contains()
在我添加它时总是返回true,这意味着ArrayList总是包含它,不是吗?我对
.contains()
方法有点陌生。@ProgrammingNub您正在检查您创建的包含前两个索引的子列表是否具有该值,而不是整个列表。这就是为什么我无法理解它。很好地工作。使用
get(0)
不是比
子列表(0,1)
更好吗,那么您可以使用
equals()
?记住。
.contains()
在我添加它时总是返回true,这意味着ArrayList总是包含它,不是吗?我对
.contains()
方法有点陌生。@ProgrammingNub您正在检查您创建的包含前两个索引的子列表是否具有该值,而不是整个列表。这就是为什么我无法理解它。很好地工作。使用
get(0)
不是比
子列表(0,1)
更好吗,那么您可以使用
equals()
?记住。你应该在
团队
数组中的团队名称旁边添加
资产.teamname
,然后你可以删除所有
if equals()
语句。@KenY-N那么我该如何绘制徽标,请多解释一点。使用类似于
公共类团队{public String teamname;public BufferedImage logo;}
private static ArrayList teams=new ArrayList()
团队。添加(“新英格兰爱国者(0-1)”,资产。爱国者)。。。等等,
g.drawImage(Game.getTeams().get(0).logo,400400,null);g、 drawImage(Game.getTeams().get(1).logo,400400,null)
@KenY-N请将其作为答案。您应该在
团队
数组中的团队名称旁边添加
资产。团队名称
,然后您可以删除所有
if equals()
语句。@KenY-N那么我该如何绘制徽标,请再解释一下。使用类似
公共类团队的东西{public String teamName;public buffereImage logo;}
私有静态ArrayList团队=新ArrayList();
团队。添加(“新英格兰爱国者(0-1)”,资产。爱国者);…等等。
,和
g.drawImage(Game.getTeams().get(0)logo,400,400,null);g.drawImage(Game.getTeams().get(1).logo,400,null)
@KenY-N请把它写进答案中。