2015-11-14 111 views
-1

有没有办法从ArrayList中“提取”单个字符串? 我已经将字符串存储在一个ArrayList中,并希望将它们打印到控制台。 我知道我可以使用for-loop,但并不是那么简单。我正在尝试创建一个基于列的打印,并使用一个方法(由堆栈溢出中的“CandiedOrange”创建),该方法以逗号分隔的字符串作为输入。从ArrayList到单个字符串的字符串JAVA

它基本上做的是什么;它根据每列中字符串的长度创建列间距。 (所有信贷“CandiedOrange”)

List<List<String>> lines = new ArrayList<>(); 
List<Integer> maxLengths = new ArrayList<>(); 
int numColumns = -1; 

public Columns addLine(String... line) { 

    if (numColumns == -1){ 
     numColumns = line.length; 
     for(int i = 0; i < numColumns; i++) { 
      maxLengths.add(0); 
     } 
    } 

    if (numColumns != line.length) { 
     throw new IllegalArgumentException(); 
    } 

    for(int i = 0; i < numColumns; i++) { 
     maxLengths.set( i, Math.max(maxLengths.get(i), line[i].length()) ); 
    } 

    lines.add(Arrays.asList(line)); 

    return this; 
} 

我要打印的列数是在编译时未知的,因为用户在运行时进入1-5列数。所以我想我可以使用ArrayLists作为行,并且使用addLine()方法来处理每行的数组列表。

如果有更好的解决方法,我会很乐意知道。

编辑:

从开始:

我创建一个Yahtzee的游戏玩家1-5。每位玩家由类“玩家”

public class Player { 

private String name; 

private int ones; 
private int twos; 
private int threes; 
private int fours; 
private int fives; 
private int sixes; 

private int threeofakind; 
private int fourofakind; 
private int fullhouse; 
private int smallstraight; 
private int largestraight; 
private int chance; 
private int yahtzee; 

private int totalscore; 

public int getOnes() { 
    return ones; 
} 

public void setOnes(int ones) { 
    this.ones = ones; 
} 

public int getTwos() { 
    return twos; 
} 

public void setTwos(int twos) { 
    this.twos = twos; 
} 

public int getThrees() { 
    return threes; 
} 

public void setThrees(int threes) { 
    this.threes = threes; 
} 

public int getFours() { 
    return fours; 
} 

public void setFours(int fours) { 
    this.fours = fours; 
} 

public int getFives() { 
    return fives; 
} 

public void setFives(int fives) { 
    this.fives = fives; 
} 

public int getSixes() { 
    return sixes; 
} 

public void setSixes(int sixes) { 
    this.sixes = sixes; 
} 

public int getThreeofakind() { 
    return threeofakind; 
} 

public void setThreeofakind(int threeofakind) { 
    this.threeofakind = threeofakind; 
} 

public int getFourofakind() { 
    return fourofakind; 
} 

public void setFourofakind(int fourofakind) { 
    this.fourofakind = fourofakind; 
} 

public int getFullhouse() { 
    return fullhouse; 
} 

public void setFullhouse(int fullhouse) { 
    this.fullhouse = fullhouse; 
} 

public int getSmallstraight() { 
    return smallstraight; 
} 

public void setSmallstraight(int smallstraight) { 
    this.smallstraight = smallstraight; 
} 

public int getLargestraight() { 
    return largestraight; 
} 

public void setLargestraight(int largestraight) { 
    this.largestraight = largestraight; 
} 

public int getChance() { 
    return chance; 
} 

public void setChance(int chance) { 
    this.chance = chance; 
} 

public int getYahtzee() { 
    return yahtzee; 
} 

public void setYahtzee(int yahtzee) { 
    this.yahtzee = yahtzee; 
} 

public int getTotalscore() { 
    return totalscore; 
} 

public void setTotalscore(int totalscore) { 
    this.totalscore = totalscore; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

}

我实现MVC结构(尽我的能力),这意味着我有一个视图类与方法的情况下定义显示主控制台列记分板。 (方法未完成)

public void displayMainScoreBoard(ArrayList<Player> playerList) { 

    Columns col = new Columns();        //Instance of column class. 

    ArrayList<String> Name = new ArrayList<>(); 
    ArrayList<Integer> Ones = new ArrayList<>(); 
    ArrayList<Integer> Twos = new ArrayList<>(); 
    ArrayList<Integer> Threes = new ArrayList<>(); 
    ArrayList<Integer> Fours = new ArrayList<>(); 
    ArrayList<Integer> Fives = new ArrayList<>(); 
    ArrayList<Integer> Sixes = new ArrayList<>(); 

    ArrayList<Integer> Threeofakind = new ArrayList<>(); 
    ArrayList<Integer> Fourofakind = new ArrayList<>(); 
    ArrayList<Integer> Fullhouse = new ArrayList<>(); 
    ArrayList<Integer> Smallstraight = new ArrayList<>(); 
    ArrayList<Integer> Largestraight = new ArrayList<>(); 
    ArrayList<Integer> Chance = new ArrayList<>(); 
    ArrayList<Integer> Yahtzee = new ArrayList<>(); 

    ArrayList<Integer> Totalscore = new ArrayList<>(); 

    for (Player p : playerList) {       //For every player, append their category data. 
     Name.add(p.getName()); 
     Ones.add(p.getOnes()); 
     Twos.add(p.getTwos()); 
     Threes.add(p.getThrees()); 
     Fours.add(p.getFours()); 
     Fives.add(p.getFives()); 
     Sixes.add(p.getSixes()); 

     Threeofakind.add(p.getThreeofakind()); 
     Fourofakind.add(p.getFourofakind()); 
     Fullhouse.add(p.getFullhouse()); 
     Smallstraight.add(p.getSmallstraight()); 
     Largestraight.add(p.getLargestraight()); 
     Chance.add(p.getChance()); 
     Yahtzee.add(p.getYahtzee()); 
     Totalscore.add(p.getTotalscore()); 
    } 

} 

并继承人CandiedOrange的全列类。 (同样,我要求他的代码没有任何权利。)

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 


public class Columns { 

List<List<String>> lines = new ArrayList<>(); 
List<Integer> maxLengths = new ArrayList<>(); 
int numColumns = -1; 

public Columns addLine(String... line) { 

    if (numColumns == -1){ 
     numColumns = line.length; 
     for(int i = 0; i < numColumns; i++) { 
      maxLengths.add(0); 
     } 
    } 

    if (numColumns != line.length) { 
     throw new IllegalArgumentException(); 
    } 

    for(int i = 0; i < numColumns; i++) { 
     maxLengths.set( i, Math.max(maxLengths.get(i), line[i].length()) ); 
    } 

    lines.add(Arrays.asList(line)); 

    return this; 
} 

public void print(){ 
    System.out.println(toString()); 
} 

public String toString(){ 
    String result = ""; 
    for(List<String> line : lines) { 
     for(int i = 0; i < numColumns; i++) { 
      result += pad(line.get(i), maxLengths.get(i) + 1); 
     } 
     result += System.lineSeparator(); 
    } 
    return result; 
} 

private String pad(String word, int newLength){ 
    while (word.length() < newLength) { 
     word += " "; 
    } 
    return word; 
} 

}

我如何通过我的ArrayList的字符串在我看来,到addLine()方法?

+0

我认为你应该从头开始。 'ArrayList'包含什么?数据来自哪里?一旦用户输入列数,您希望如何打印?这是用户输入的唯一参数吗?如果您解释实际问题,而不仅仅是具体解决方案无效的事实,那么我们可以提供不同的解决方案。 – RealSkeptic

+0

会做,给我一分钟。 – Danny

回答

0

我相信你不应该使用那个Columns类。

大多数列表是数字而不是字符串。数字最好显示正确。并且您想要显示的每行中唯一的字符串是玩家名称。

因此,而不是复制所有数据,以各种列表,并假设大多数的数字你有一个合理的宽度,他们不会通过(?4位6位),然后你的任务就变成了:

  • 查找列表中最长的名字
  • 显示每个玩家,使名称被填充以容纳找到的最长名称。

你可以添加为方法的Player类,所以寻找的球员定列表的最大名称长度:

public static int maxNameLength(List<? extends Player> players) { 
    int maxLength = 0; 
    for (Player player : players) { 
     int currLength = player.getName().length(); 
     if (currLength > maxLength) { 
      maxLength = currLength; 
     } 
    } 
    return maxLength; 
} 

现在,展现在适当填充的方式获得当前玩家可以使用String.format完成,其使用Formatter

为简洁起见,假设我只想显示名称,Yahtzee和总分。你将有一个这样的方法在Player

public String getScoreLine(int maxLength) { 

    String format = "%-" + maxLength + "s %6d %6d"; 

    return String.format(format, getName(), getYahtzee(), getTotalscore()); 
} 

什么第一部分做的是创建一个左对齐字段的字符串。所以如果maxLength是20,那么格式将是%-20s %6d %6d。数字将在6个字符宽的字段中右对齐显示,名称左对齐并填充为20个字符。

现在你可以循环清单上并显示它想:

int maxNameLength = Player.maxNameLength(playerList); 
for (Player p : playerList) { 
    System.out.println(p.getScoreLine(maxNameLength)); 
} 

注意:如果你希望把你的视图,而不是在Player类的getScoreLine方法(因为你正在尝试做的MVC ),你需要把它作为一个参数给玩家。

0

...以逗号分隔的字符串作为输入。

从技术上讲,它需要一个数组(more in this Java tutorial on varargs)。但是,Java允许您在使用离散参数调用它时自动创建该数组。

因为它接受一个数组,和你有一个ArrayList,你可以很容易地得到一个数组传递给它,使用toArray(T[])

List<List<String>> linesToAdd = /*...*/; 
// ... 
for (List<String> line : linesToAdd) { 
    addLine(line.toArray(new String[line.size()])); 
} 

这就是说,这将是微不足道的(和良好的编码练习)修改addLine直接接受List<String>

0

我按照建议不使用列类,即使我想尝试使用它。我对解决方案进行了半硬编码。玩家列表是玩家在游戏中出现的列表。并且从另一个班级获得类别以检查评分时可以选择哪些类别。 (显示为X时不可和O拣选时)

public void displayMainScoreBoard(ArrayList<Player> playerList, ArrayList<Boolean> categories) { 

    List<String> Name = new ArrayList<>(); 
    List<String> Ones = new ArrayList<>(); 
    List<String> Twos = new ArrayList<>(); 
    List<String> Threes = new ArrayList<>(); 
    List<String> Fours = new ArrayList<>(); 
    List<String> Fives = new ArrayList<>(); 
    List<String> Sixes = new ArrayList<>(); 

    List<String> Threeofakind = new ArrayList<>(); 
    List<String> Fourofakind = new ArrayList<>(); 
    List<String> Fullhouse = new ArrayList<>(); 
    List<String> Smallstraight = new ArrayList<>(); 
    List<String> Largestraight = new ArrayList<>(); 
    List<String> Chance = new ArrayList<>(); 
    List<String> Yahtzee = new ArrayList<>(); 

    List<String> Totalscore = new ArrayList<>(); 

    for (Player p : playerList) {       //For every player, append their category data. 
     Name.add(p.getName()); 
     Ones.add(String.valueOf(p.getOnes())); 
     Twos.add(String.valueOf(p.getTwos())); 
     Threes.add(String.valueOf(p.getThrees())); 
     Fours.add(String.valueOf(p.getFours())); 
     Fives.add(String.valueOf(p.getFives())); 
     Sixes.add(String.valueOf(p.getSixes())); 

     Threeofakind.add(String.valueOf(p.getThreeofakind())); 
     Fourofakind.add(String.valueOf(p.getFourofakind())); 
     Fullhouse.add(String.valueOf(p.getFullhouse())); 
     Smallstraight.add(String.valueOf(p.getSmallstraight())); 
     Largestraight.add(String.valueOf(p.getLargestraight())); 
     Chance.add(String.valueOf(p.getChance())); 
     Yahtzee.add(String.valueOf(p.getYahtzee())); 
     Totalscore.add(String.valueOf(p.getTotalscore())); 
    } 

    boolean checkones = categories.get(0);    //Checkers for which categories are available to score in. 
    boolean checktwos = categories.get(1); 
    boolean checkthrees = categories.get(2); 
    boolean checkfours = categories.get(3); 
    boolean checkfives = categories.get(4); 
    boolean checksixes = categories.get(5); 
    boolean checkthreeofkind = categories.get(6); 
    boolean checkfourofkind = categories.get(7); 
    boolean checkfullhouse = categories.get(8); 
    boolean checksmallstraight = categories.get(9); 
    boolean checklargestraight = categories.get(10); 
    boolean checkchance = categories.get(11); 
    boolean checkyahtzee = categories.get(12); 



    System.out.println("| "+         "| | Name   " + stringBuilder(Name)); 
    System.out.println("|" + isPickable(checkones) +   "|1. | Ones   " + stringBuilder(Ones)); 
    System.out.println("|" + isPickable(checktwos) +   "|2. | Twos   " + stringBuilder(Twos)); 
    System.out.println("|" + isPickable(checkthrees) +  "|3. | Threes   " + stringBuilder(Threes)); 
    System.out.println("|" + isPickable(checkfours) +   "|4. | Fours   " + stringBuilder(Fours)); 
    System.out.println("|" + isPickable(checkfives) +   "|5. | Fives   " + stringBuilder(Fives)); 
    System.out.println("|" + isPickable(checksixes) +   "|6. | Sixes   " + stringBuilder(Sixes)); 
    System.out.println("|" + isPickable(checkthreeofkind) + "|7. | Three of a kind " + stringBuilder(Threeofakind)); 
    System.out.println("|" + isPickable(checkfourofkind) + "|8. | Four of a kind " + stringBuilder(Fourofakind)); 
    System.out.println("|" + isPickable(checkfullhouse) +  "|9. | Full House  " + stringBuilder(Fullhouse)); 
    System.out.println("|" + isPickable(checksmallstraight) + "|1. | Small Straight " + stringBuilder(Smallstraight)); 
    System.out.println("|" + isPickable(checklargestraight) + "|11.| Large Straight " + stringBuilder(Largestraight)); 
    System.out.println("|" + isPickable(checkchance) +  "|12.| Chance   " + stringBuilder(Chance)); 
    System.out.println("|" + isPickable(checkyahtzee) +  "|13.| Yahtzee   " + stringBuilder(Yahtzee)); 
    System.out.println("| "+         "| | Total Score  " + stringBuilder(Totalscore)); 

} 

/* 
* Method for creating the lines for the scoreboard. 
*/ 
public String stringBuilder(List<String> arrIn) { 
    StringBuilder sb = new StringBuilder(); 
    for (String s : arrIn) { 
     sb.append("|"); 
     sb.append(s); 
     sb.append(pad(s)); 
    } 
    return sb.toString(); 
} 

/* 
* Method for padding the spaces between columns. 
*/ 
public String pad(String s) { 
    int space = 15; 
    int sLength = s.length(); 
    String retPad = ""; 
    int temp = space - sLength; 

    for (int i = 0; i <= temp ; i++) { 
     retPad += " "; 
    } 
    return retPad; 
} 

public String isPickable(boolean b) { 
    if (b == true) { 
     return "O"; 
    } 
    else { 
     return "X"; 
    } 
} 

给出了这样的打印输出:

| | | Name   |one    |two    |three   
|X|1. | Ones   |0    |0    |0    
|X|2. | Twos   |0    |0    |0    
|X|3. | Threes   |0    |0    |0    
|X|4. | Fours   |0    |0    |0    
|X|5. | Fives   |0    |0    |0    
|X|6. | Sixes   |0    |0    |0    
|X|7. | Three of a kind |0    |0    |0    
|X|8. | Four of a kind |0    |0    |0    
|X|9. | Full House  |0    |0    |0    
|X|1. | Small Straight |0    |0    |0    
|X|11.| Large Straight |0    |0    |0    
|X|12.| Chance   |0    |0    |0    
|X|13.| Yahtzee   |0    |0    |0    
| | | Total Score  |0    |0    |0 

这样看起来不错,只要用户不输入冗长得可笑的名字。这可以通过要求用户输入一个较短的名称(昵称)来解决。 无论如何,感谢您的帮助!