java - 如何测试 ArrayList<String> 中的某个项目是否位于某个位置

标签 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) 等于 "Arizona Cardinals" 或我与之合作的 3 支球队之一时到目前为止,屏幕上没有任何内容。
我的所有类(class)如下:
我的主要类(class):

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;
    }

}

我的 GameII 类:

import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import foot.display.Display;
import foot.gfx.Assets;

public class GameII implements Runnable {

    private Display display;
    public int width, height;
    public String title;

    private boolean running = false;
    private Thread thread;

    private BufferStrategy bs;
    private Graphics g;

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

    private void init(){
        display = new Display(title, width, height);
        Assets.init();
    }

    private void tick(){

    }

    private void render(){
        bs = display.getCanvas().getBufferStrategy();
        if(bs == null){
            display.getCanvas().createBufferStrategy(3);
            return;
        }
        g = bs.getDrawGraphics();
        //Clear Screen
        g.clearRect(0, 0, width, height);
        //Draw Here!

        // team 1's
        if(Game.getTeams().subList(0, 1).equals("Arizona Cardinals (0 - 1)"))
            g.drawImage(Assets.cardinals, 100, 100, null);
        if(Game.getTeams().subList(0, 1).equals("Jacksonville Jaguars (1 - 0)))
            g.drawImage(Assets.jaguars, 100, 100, null);
        if(Game.getTeams().subList(0, 1).equals("New England Patriots (0 - 1)"))
            g.drawImage(Assets.patriots, 100, 100, null);

        // team 2's
        if(Game.getTeams().subList(1, 2).equals("Arizona Cardinals (0 - 1)"))
            g.drawImage(Assets.cardinals, 400, 400, null);
        if(Game.getTeams().subList(1, 2).equals("Jacksonville Jaguars (1 - 0)"))
            g.drawImage(Assets.jaguars, 400, 400, null);
        if(Game.getTeams().subList(1, 2).equals("New England Patriots (0 - 1)"))
            g.drawImage(Assets.patriots, 400, 400, null);

        //End Drawing!
        bs.show();
        g.dispose();
    }

    public void run(){

        init();

        while(running){
            tick();
            render();
        }

        stop();

    }

    public synchronized void start(){
        if(running)
            return;
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public synchronized void stop(){
        if(!running)
            return;
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

我的 Assets 类别:

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;
    }

}

这些是我的所有类(class),应该足以重现问题。
只想说:
从理论上/逻辑上来说,我的公式应该有效。
如果你仔细想想,我的步骤是完全合乎逻辑的:
1) 创建一个字符串数组列表并将所有 NFL 球队的名称添加到其中
2)随机排列ArrayList
3) 随机选择其中两个“球队”进行“比赛”
4) 测试是否选择了特定团队
5) 如果是这样,则将该球队的标志绘制到之前制作的 JFrame

最佳答案

这是很多代码。但我认为你想使用 contains 而不是 equals:

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

关于java - 如何测试 ArrayList<String> 中的某个项目是否位于某个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47148788/

相关文章:

java swing 认证失败

java - 通过 div id 更新 html JTextPane?

php - 变量中的 If 逻辑(谓词表达式)

c++ - 用户定义类中的 Bool 作为函数 c++ 出现问题

java - ColdFusion - java对象方法调用

java - 为 Loading Cache guava 编写测试用例

java - 为什么模拟 mongotemplate.aggregate() 返回 NullPointerException [Spring Boot Mockito]

java - 为什么 .array() 对从映射的 FileChannels 返回的 ByteBuffers 不起作用?

java - 对 JTable 的内容进行排序

C++, 'if' 表达式中的变量声明