【问题标题】:How to test if a certain item in an ArrayList<String> is in a certain place如何测试 ArrayList<String> 中的某个项目是否在某个位置
【发布时间】:2018-04-19 07:33:47
【问题描述】:

现在我正在做一个看起来像这样的 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 个团队之一,屏幕上都没有任何内容。
我所有的课程都在下面:
我的主要课程:

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

}

我的资产类:

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

}

还有我的 Display 类:

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) 洗牌 ArrayList
3)随机选择其中两个“团队”进行“比赛”
4) 测试是否选择了特定团队
5)如果是这样,将那个团队的标志画到之前制作的JFrame上

【问题讨论】:

  • 您应该在teams数组中的团队名称旁边添加Assets.teamname,然后您可以删除所有if equals()语句。
  • @KenY-N 那么我将如何绘制徽标,请解释一下。
  • public class Team { public String teamName; public BufferedImage logo; }private static ArrayList&lt;Team&gt; teams = new ArrayList&lt;Team&gt;();teams.add("New England Patriots (0 - 1)", Assets.patriots); ...etc...g.drawImage(Game.getTeams().get(0).logo, 400, 400, null); g.drawImage(Game.getTeams().get(1).logo, 400, 400, null); 之类的东西。
  • @KenY-N 请将其放入答案中。

标签: java swing if-statement arraylist


【解决方案1】:

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

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

【讨论】:

  • .contains() 在我添加它时总是会返回 true,这意味着 ArrayList 将始终包含它,不是吗?我对.contains() 方法有点陌生。
  • @ProgrammingNub 您正在检查您创建的包含前两个索引的子列表是否具有该值,而不是整个列表。
  • 这就是我想不通的原因。完美运行。
  • get(0)不会比subList(0, 1)好,那你可以用equals()吗?记住subList() is exclusive of the upper index
猜你喜欢
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
相关资源
最近更新 更多