有没有办法复制像这样的字符串并将其放入数组?
String s =“Hello”; - > String [] s = {“Hello”,“Hello”};
数组必须复制s中的内容。
答案 0 :(得分:1)
您可以创建一个类似
的方法public class MyGraph{
//filled with strings from file
String[] points;
java.io.File file;
java.util.Scanner input;
//length of points array
int numPoints;
public MyGraph(String file){
this.file = new java.io.File(file);
this.input = new java.util.Scanner(file);
this.numPoints = this.input.nextInt();
this.points = new String[this.numPoints];
fillGraphArray();
}
//after getting the number of vertices we populate the array with every
//line after those points untill the end
private void fillGraphArray(){
//used once after reading nextInt()
this.input.nextLine();
int count = 0;
while(this.input.hasNext() == true){
points[count] = input.nextLine();
count++;
}
input.close();
}
//test method to be delted later
public String[] getPoints(){
return this.points;
}
//may need a method to close the file
}
答案 1 :(得分:0)
你可以这样做:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Mar 07 22:48:57 UTC 2018
There was an unexpected error (type=Internal Server Error, status=500).
Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
或:
String[] resultSet = IntStream.rangeClosed(1, n)
.mapToObj(i -> s)
.toArray(String[]::new);
其中String[] resultSet = Stream.generate(() -> s)
.limit(n)
.toArray(String[]::new);
是您要生成字符串n
的次数。
示例:
s
会打印:
int n = 2;
String s = "Hello";
String[] resultSet = IntStream.rangeClosed(1, n)
.mapToObj(i -> s)
.toArray(String[]::new);
System.out.println(Arrays.toString(resultSet));