Teach you how to use List to transfer student information
Collections are often used in program development. For example, in business methods, student information, product information, etc. are stored in collections, and then returned to the caller as the return value of the method, thereby passing a large amount of ordered data.
This example will use the List collection to transfer student information between methods. The example effect is as follows:
Design process
1) Create a new form class ClassInfo in the project. Add JScrollPane to the form, and then place the table control JTable in the JScrollPane.
2) Write the getTable() method. In this method, create a table object and set the data model of the table, and then call the getStudents() method to obtain the collection object of student information.
While traversing the collection object, add each element to the table model rows and displayed in the table control.
1 private JTable getTable() { 2 if (table == null) { 3 table = new JTable();// 创建表格控件 4 table.setRowHeight(23);// 设置行高度 5 String[] columns = { "姓名", "性别", "出生日期" };// 创建列名数组 6 // 创建表格模型 7 DefaultTableModel model = new DefaultTableModel(columns, 0); 8 table.setModel(model);// 设置表格模型 9 List<String> students = getStudents();// 调用方法传递list集合对象 10 for (String info : students) {// 遍历学生集合对象 11 String[] args = info.split(",");// 把学生信息拆分为数组 12 model.addRow(args);// 把学生信息添加到表格的行 13 } 14 } 15 return table; 16 }
3) Write the getStudents() method, which will pass the List collection object to the caller and add multiple elements to the collection object. Each element value is a student information. , which includes name, gender, and date of birth.
1 private List<String> getStudents(){ 2 //创建List集合对象 3 List<String> list = new ArrayList<String>(); 4 list.add("钱东强,男,1990-1-4");//字符串之间用英文的逗号隔开 5 list.add("孙西,女,1990-1-4"); 6 list.add("周五燕,男,1990-1-4"); 7 list.add("王东,女,1990-1-4"); 8 list.add("冯晓,男,1990-1-4"); 9 list.add("陈玉山,女,1990-1-4"); 10 list.add("蒋钦大,男,1990-1-4"); 11 return list; 12 13 }
Full code
1 package cn.str.opera; 2 3 import java.awt.BorderLayout; 4 5 public class ClassInfo extends JFrame { 6 7 private JPanel contentPane; 8 private JTable table; 9 10 /** 11 * Launch the application. 12 */ 13 public static void main(String[] args) { 14 15 try { 16 UIManager 17 .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 18 } catch (Throwable e) { 19 e.printStackTrace(); 20 } 21 EventQueue.invokeLater(new Runnable() { 22 public void run() { 23 try { 24 ClassInfo frame = new ClassInfo(); 25 frame.setVisible(true); 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } 29 } 30 }); 31 } 32 33 /** 34 * Create the frame. 35 */ 36 public ClassInfo() { 37 getContentPane().setLayout(new BorderLayout(0, 0)); 38 setTitle("\u7528List\u96C6\u5408\u4F20\u9012\u5B66\u751F\u4FE1\u606F"); 39 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 40 setBounds(100, 100, 450, 300); 41 contentPane = new JPanel(); 42 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 43 contentPane.setLayout(new BorderLayout(0, 0)); 44 setContentPane(contentPane); 45 46 47 JScrollPane scrollPane = new JScrollPane(); 48 contentPane.add(scrollPane,BorderLayout.CENTER); 49 scrollPane.setViewportView(getTable()); 50 51 } 52 53 private JTable getTable() { 54 if(table == null){ 55 table = new JTable();//创建表格控件 56 table.setRowHeight(23); 57 String[] columns = {"姓名","性别","出生日期"};//创建列名数组 58 //创建表格模型 59 DefaultTableModel model = new DefaultTableModel(columns,0); 60 table.setModel(model);//设置表格模型 61 List<String> students = getStudents(); 62 for(String info:students){// 遍历学生集合对象 63 String[] args = info.split(",");// 按英文逗号,把学生信息拆分为数组 64 model.addRow(args); 65 } 66 67 } 68 return table; 69 } 70 71 private List<String> getStudents(){ 72 //创建List集合对象 73 List<String> list = new ArrayList<String>(); 74 list.add("钱东强,男,1990-1-4");//字符串之间用英文的逗号隔开 75 list.add("孙西,女,1990-1-4"); 76 list.add("周五燕,男,1990-1-4"); 77 list.add("王东,女,1990-1-4"); 78 list.add("冯晓,男,1990-1-4"); 79 list.add("陈玉山,女,1990-1-4"); 80 list.add("蒋钦大,男,1990-1-4"); 81 return list; 82 83 } 84 85 }
The above is the detailed content of Teach you how to use List to transfer student information. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Microsoft Word documents contain some metadata when saved. These details are used for identification on the document, such as when it was created, who the author was, date modified, etc. It also has other information such as number of characters, number of words, number of paragraphs, and more. If you might want to remove the author or last modified information or any other information so that other people don't know the values, then there is a way. In this article, let’s see how to remove a document’s author and last modified information. Remove author and last modified information from Microsoft Word document Step 1 – Go to

Delivery Optimization is a feature that helps Windows Update and Windows Store run and deliver updates faster. Cache files in Delivery Optimization are supposed to be deleted after a while, but for some of our readers they keep piling up and taking up unnecessary space. Is it safe to delete delivery optimization files? Yes, it is safe to delete delivery optimization files, and in this article, you will find out how easy it is to do so in Windows 11. Although it is not recommended to manually delete delivery optimization files, it is possible to do so automatically. How to delete delivery optimization files on Windows 11? Click the search bar, type Disk Cleanup, and open the tool from the results. If you have multiple drives, select the drive with your system (usually C:

Using System Information Click Start and enter System Information. Just click on the program as shown in the image below. Here you can find most of the system information, and one thing you can find is graphics card information. In the System Information program, expand Components, and then click Show. Let the program gather all the necessary information and once it's ready, you can find the graphics card-specific name and other information on your system. Even if you have multiple graphics cards, you can find most content related to dedicated and integrated graphics cards connected to your computer from here. Using the Device Manager Windows 11 Just like most other versions of Windows, you can also find the graphics card on your computer from the Device Manager. Click Start and then

In iOS 17, there's a new AirDrop feature that lets you exchange contact information with someone by touching two iPhones. It's called NameDrop, and here's how it works. Instead of entering a new person's number to call or text them, NameDrop allows you to simply place your iPhone near their iPhone to exchange contact details so they have your number. Putting the two devices together will automatically pop up the contact sharing interface. Clicking on the pop-up will display a person's contact information and their contact poster (you can customize and edit your own photos, also a new feature of iOS17). This screen also includes the option to "Receive Only" or share your own contact information in response.

List operation //Insert a value from the head of the list. $ret=$redis->lPush('city','guangzhou');//Insert a value from the end of the list. $ret=$redis->rPush('city','guangzhou');//Get the elements in the specified range of the list. 0 represents the first element of the list, -1 represents the last element, and -2 represents the penultimate element. $ret=$redis->l

Current image 3D reconstruction work usually uses a multi-view stereo reconstruction method (Multi-view Stereo) that captures the target scene from multiple viewpoints (multi-view) under constant natural lighting conditions. However, these methods usually assume Lambertian surfaces and have difficulty recovering high-frequency details. Another approach to scene reconstruction is to utilize images captured from a fixed viewpoint but with different point lights. Photometric Stereo methods, for example, take this setup and use its shading information to reconstruct the surface details of non-Lambertian objects. However, existing single-view methods usually use normal map or depth map to represent the visible

In iOS17, there is a new AirDrop feature that allows you to exchange contact information with someone by touching two iPhones at the same time. It's called NameDrop, and here's how it actually works. NameDrop eliminates the need to enter a new person's number to call or text them so they have your number, you can simply hold your iPhone close to their iPhone to exchange contact information. Putting the two devices together will automatically pop up the contact sharing interface. Clicking on the popup will display a person's contact information and their contact poster (a photo of your own that you can customize and edit, also new to iOS 17). This screen also includes "Receive Only" or share your own contact information in response

The reason for the delay in WeChat receiving information may be network problems, server load, version problems, device problems, message sending problems or other factors. Detailed introduction: 1. Network problems. The delay in receiving information on WeChat may be related to the network connection. If the network connection is unstable or the signal is weak, it may cause delays in information transmission. Please ensure that the mobile phone is connected to a stable network and the network signal strength is good. ; 2. Server load. When the WeChat server load is high, it may cause delays in information transmission, especially during busy periods or when a large number of users use WeChat at the same time, etc.
