有没有办法从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()方法?
我认为你应该从头开始。 'ArrayList'包含什么?数据来自哪里?一旦用户输入列数,您希望如何打印?这是用户输入的唯一参数吗?如果您解释实际问题,而不仅仅是具体解决方案无效的事实,那么我们可以提供不同的解决方案。 – RealSkeptic
会做,给我一分钟。 – Danny