java的string转换为list
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java的string转换为list相关的知识,希望对你有一定的参考价值。
string类型的 string abc = "aaa,bbb,ccc",
怎样转换为list呢
方法如下:
List<String> list = new ArrayList<String>();
list.add("a1");
list.add("a2");
String[] toBeStored = list.toArray(new String[list.size()]);
for(String s : toBeStored)
System.out.println(s);
或者是:
List<String> list = new ArrayList<String>();
list.add("a1");
list.add("a2");
String[] toBeStored = new String[al.size()];
list.toArray(toBeStored);
for (String s : toBeStored)
System.out.println(s);
拓展资料:
Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点 。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。
参考技术Aimport java.util.Arrays;
import java.util.List;
public class test
public static void main(String[] args)
// 字符串转list<String>
String str = "aaa,bbb,ccc";
List<String> lis = Arrays.asList(str.split(","));
for (String string : lis)
System.out.println(string);
拓展资料:
List:元素是有序的(怎么存的就怎么取出来,顺序不会乱),元素可以重复(角标1上有个3,角标2上也可以有个3)因为该集合体系有索引;
ArrayList:底层的数据结构使用的是数组结构(数组长度是可变的百分之五十延长)(特点是查询很快,但增删较慢)线程不同步;
LinkedList:底层的数据结构是链表结构(特点是查询较慢,增删较快);
Vector:底层是数组数据结构 线程同步(数组长度是可变的百分之百延长)。
String[] arr = abc.split(",");
List<String> list = java.util.Arrays.aslist(abc);本回答被提问者采纳 参考技术C 假设String对象为stringArray;
List<T> list=JSONArray.parseArray("",T.class);
其中T为要转为的list其中的对象,比如创建的实体类。 参考技术D String[] arr= abc.split(",");
List list = Arrays.asList(arr);
无法将类型“java.lang.String”的属性值转换为属性“事务”所需的类型“java.util.List”
【中文标题】无法将类型“java.lang.String”的属性值转换为属性“事务”所需的类型“java.util.List”【英文标题】:Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'transactions' 【发布时间】:2021-10-27 17:30:35 【问题描述】:我正在使用 thymeleaf 并在将数据从 String 转换为 List 时遇到一些错误。在这里我附上了我的代码
我的实体类:
@Entity
@Table(name="customer")
public class Customer
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "first_name")
@NotNull(message = "First Name cannot be empty")
@Size(min = 1, message = "First Name cannot be empty")
private String firstName;
@Column(name = "last_name")
@NotNull(message = "Last Name cannot be empty")
@Size(min = 1, message = "Last Name cannot be empty")
private String lastName;
@Column(name = "email")
@NotNull(message = "Email ID cannot be empty")
@Pattern(regexp = "^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9.-]+$",
message = "Enter valid mail id")
private String email;
@Column(name = "branch")
@NotNull(message = "Branch name cannot be empty")
private String branch;
@Column(name = "balance")
private double balance;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id")
private List<Transaction> transactions;
public Customer(String firstName, String lastName, String email, String branch, double balance)
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.branch = branch;
this.balance = balance;
public Customer()
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getFirstName()
return firstName;
public void setFirstName(String firstName)
this.firstName = firstName;
public String getLastName()
return lastName;
public void setLastName(String lastName)
this.lastName = lastName;
public String getEmail()
return email;
public void setEmail(String email)
this.email = email;
public String getBranch()
return branch;
public void setBranch(String branch)
this.branch = branch;
public double getBalance()
return balance;
public void setBalance(double balance)
this.balance = balance;
public List<Transaction> getTransactions()
return transactions;
public void setTransactions(List<Transaction> transactions)
this.transactions = transactions;
public void addTransaction(Transaction transaction)
if(transaction == null)
transactions = new ArrayList<>();
transactions.add(transaction);
@Override
public String toString()
return "Customer" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", branch='" + branch + '\'' +
", balance=" + balance +
'';
@Entity
@Table(name = "transactions")
public class Transaction
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "transaction")
private double amount;
public Transaction()
public Transaction(double amount)
this.amount = amount;
public int getId()
return id;
public void setId(int id)
this.id = id;
public double getAmount()
return amount;
public void setAmount(double amount)
this.amount = amount;
@Override
public String toString()
return "Transaction" +
"id=" + id +
", amount=" + amount +
'';
控制器:
@Controller
@RequestMapping("/customers")
public class CustomerController
@Autowired
private CustomerRestService customerRestService;
public static String username;
public static Object password;
public CustomerController()
@GetMapping("/list")
public String listCustomers(Model model, @CurrentSecurityContext(expression = "authentication")Authentication authentication)
username = authentication.getName();
password = authentication.getCredentials();
List<Customer> customers = customerRestService.getCustomerList();
model.addAttribute("customers", customers);
return "list-customers";
@GetMapping("/showFormToAddCustomer")
public String showFormToAddCustomer(Model model)
Customer customer = new Customer();
model.addAttribute("customer", customer);
return "customer-form";
@PostMapping("/saveCustomer")
public String saveCustomer(@ModelAttribute("customer") Customer customer)
System.out.println("\n" + customer);
System.out.println(customer.getTransactions());
customerRestService.saveCustomer(customer);
return "redirect:/customers/list";
@GetMapping("/showFormToUpdateCustomer")
public String showformForUpdate(@RequestParam("customerId") int id,
Model model)
Customer customer = customerRestService.findCustomerById(id);
System.out.println(customer);
System.out.println(customer.getTransactions());
model.addAttribute("customer", customer);
return "customer-form";
服务:
@Service
public class CustomerRestServiceImple implements CustomerRestService
private RestTemplate restTemplate;
private String restUrl;
@Autowired
public CustomerRestServiceImple(RestTemplate restTemplate,
@Value("$crm.rest.url") String restUrl)
this.restTemplate = restTemplate;
this.restUrl = restUrl;
private HttpHeaders httpHeaders()
String username = CustomerController.username;
Object password = CustomerController.password;
String plainCreds = username+":"+password.toString();
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
//HttpEntity<String> request = new HttpEntity<String>(headers);
return headers;
@Override
public List<Customer> getCustomerList()
HttpEntity<String> request = new HttpEntity<>(httpHeaders());
ResponseEntity<List<Customer>> responseEntity = restTemplate.exchange(restUrl, HttpMethod.GET, request,
new ParameterizedTypeReference<List<Customer>>() );
List<Customer> customers = responseEntity.getBody();
return customers;
@Override
public Customer findCustomerById(int id)
//Customer customer = restTemplate.getForObject(restUrl + "/" + id, Customer.class);
HttpEntity<String> request = new HttpEntity<>(httpHeaders());
ResponseEntity<Customer> responseEntity = restTemplate.exchange(restUrl + "/" + id, HttpMethod.GET, request, Customer.class);
Customer customer = responseEntity.getBody();
return customer;
@Override
public void saveCustomer(Customer customer)
System.out.println("\n" + customer);
System.out.println(customer.getTransactions());
HttpEntity<Customer> request = new HttpEntity<>(customer, httpHeaders());
int customerId = customer.getId();
if(customerId == 0)
restTemplate.exchange(restUrl, HttpMethod.POST, request, Customer.class);
else
restTemplate.exchange(restUrl, HttpMethod.PUT, request, Customer.class);
HTML 表单:
<h3>Customer Directory</h3>
<hr>
<p class="h4 mb-4">Save Customer</p>
<form action="#" th:action="@/customers/saveCustomer"
th:object="$customer" method="POST">
<!-- Add hidden form field to handle the update -->
<input type="hidden" th:field="*id" />
<input type="text" th:field="*firstName"
class="form-control mb-4 col-4" placeholder="First Name">
<input type="text" th:field="*lastName"
class="form-control mb-4 col-4" placeholder="Last Name">
<input type="text" th:field="*email"
class="form-control mb-4 col-4" placeholder="Email">
<input type="text" th:field="*branch"
class="form-control mb-4 col-4" placeholder="Branch">
<input type="text" th:field="*balance"
class="form-control mb-4 col-4" placeholder="Balance">
<input type="text" th:field="*transactions"
class="form-control mb-4 col-4" placeholder="Transactions">
<!--input type="hidden" th:field="*transactions" /-->
<button type="submit" class="btn btn-info col-2">Save</button>
</form>
<hr>
<a th:href="@/customers/list">Back to Customers List</a>
</div>
错误:
字段“交易”的对象“客户”中的字段错误:拒绝值 [[Transactionid=1, amount=1000.0, Transactionid=3, amount=100.0]];代码 [typeMismatch.customer.transactions,typeMismatch.transactions,typeMismatch.java.util.List,typeMismatch];参数 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [customer.transactions,transactions];论据 [];默认消息[事务]];默认消息 [;嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.String] 转换为类型 [@javax.persistence.OneToMany @javax.persistence.JoinColumn com.kaneki.springboot.bankapplication.entity。交易] 价值 '[交易id=1, 金额=1000.0, 交易id=3, 金额=100.0]';嵌套异常是 java.lang.NumberFormatException: For input string: "[Transactionid=1, amount=1000.0, Transactionid=3, amount=100.0]"]
【问题讨论】:
【参考方案1】:在这里,您从您的 HTML 中接收作为字符串的 Transaction 字段,因此在控制器中您必须处理该字符串并从中提取 id 和金额,然后将该 id 和金额设置到交易表中。试试这个,如果你已经在你的控制器中尝试或编写了它,请分享控制器的代码。
【讨论】:
我在这里添加了控制器代码。能否请您再重新审视一下这个问题以上是关于java的string转换为list的主要内容,如果未能解决你的问题,请参考以下文章