public studentList(String[] file) { for(String s : file){ this.readin(s); } }

时间: 2024-03-29 16:35:55 浏览: 13
这段代码是一个构造方法,它的作用是从一个字符串数组file中读取学生信息,并将它们存储在studentList中。具体实现是通过for-each循环遍历字符串数组file中的每一个元素s,然后调用类中的readin方法读取这个学生信息,并将其存储在studentList中。由于这个构造方法的名称与类名称相同,因此它是一个默认的构造方法,当创建一个studentList对象时,会自动调用这个构造方法,并将文件中的学生信息读取到studentList中。注意,readin方法的实现在这段代码中没有给出。
相关问题

public studentList(String file) { this.readin(file); }

这段代码是一个构造方法,它的作用是从一个文件中读取学生信息,并将它们存储在studentList中。具体实现是调用类中的readin方法读取文件中的学生信息,并将其存储在studentList中。其中,readin方法的参数是文件名或文件路径。由于这个构造方法只有一个参数,且参数类型为String,因此它可以方便地从外部调用,并且可以根据需要读取不同的文件。注意,readin方法的实现在这段代码中没有给出。

public boolean addNewStudent(String[] student,int file) { StringBuffer studnetOneString = new StringBuffer(); this.readin(studentManager.studentFile[file-1]); this.studentList.add(student); for (String[] studentOneArray : studentList) { for (String studnetOne : studentOneArray) { studnetOneString.append(studnetOne + ";"); } studnetOneString.append("\n"); } ByteArrayInputStream stream = null; stream = new ByteArrayInputStream(studnetOneString.toString().getBytes()); try { OutputStream os = new FileOutputStream(studentManager.studentFile[file-1]); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); stream.close(); studentList = null; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; }

这段代码是一个方法,它的作用是向学生列表中添加一个新的学生信息,并将这个信息写入到指定的文件中。具体实现如下: 首先,创建一个StringBuffer对象studnetOneString,用于存储所有学生信息的字符串。然后调用readin方法读取指定文件中的所有学生信息,并将它们存储在studentList中。接着,向studentList中添加新的学生信息。这个新的学生信息是由一个String数组student表示的,它包含了学生的各个属性,例如姓名、学号、专业、费用等。 接下来,通过for-each循环遍历studentList中的每一个学生信息,将它们转换成一个字符串,并将这个字符串添加到studnetOneString中。这个字符串的格式是将每个属性用“;”分隔开来,并在每个学生信息的结尾添加一个换行符“\n”。 然后,创建一个ByteArrayInputStream对象stream,将studnetOneString转换成一个字节数组,并将这个字节数组作为stream的初始值。接着,创建一个输出流OutputStream os,将要写入的文件名作为参数传递给它。然后,创建一个缓冲区byte[] buffer,用于读取stream中的字节数据。接着,通过while循环不断地从stream中读取字节数据,并将这些数据写入到输出流os中。当读取到的字节数为-1时,表示已经读取完全部数据,此时退出循环。最后,关闭输入流stream和输出流os,并将studentList引用置为null。 最后,这个方法返回一个布尔值,表示添加新学生信息是否成功。如果成功添加新学生信息并将其写入到文件中,方法返回true,否则返回false。

相关推荐

The following is the data that you can add to your input file (as an example). Notice that the first line is going to be a line representing your own hobbies. In my case, it is the Vitaly,table tennis,chess,hacking line. Your goal is to create a class called Student. Every Student will contain a name (String) and an ArrayList<String> storing hobbies. Then, you will add all those students from the file into an ArrayList<Student>, with each Student having a separate name and ArrayList of hobbies. Here is an example file containing students (the first line will always represent yourself). NOTE: eventually, we will have a different file containing all our real names and hobbies so that we could find out with how many people each of us share the same hobby. Vitaly,table tennis,chess,hacking Sean,cooking,guitar,rainbow six Nolan,gym,piano,reading,video games Jack,cooking,swimming,music Ray,piano,video games,volleyball Emily,crochet,drawing,gardening,tuba,violin Hudson,anime,video games,trumpet Matt,piano,Reading,video games,traveling Alex,swimming,video games,saxophone Roman,piano,dancing,art Teddy,chess,lifting,swimming Sarah,baking,reading,singing,theatre Maya,violin,knitting,reading,billiards Amy,art,gaming,guitar,table tennis Daniel,video games,tennis,soccer,biking,trumpet Derek,cooking,flute,gaming,swimming,table tennis Daisey,video games,guitar,cleaning,drawing,animated shows,reading,shopping Lily,flute,ocarina,video games,baking Stella,roller skating,sudoku,watching baseball,harp Sophie,viola,ukulele,piano,video games Step 2. Sort the student list in the ascending order of student names and print them all on the screen After reading the file and storing the data in an ArrayList<Student>, your program should sort the ArrayList<Student> in alphabetical order based on their names and then print the students' data (please see an example below). As you can see, here is the list of all students printed in alphabetical order based on their names and hobbies. You are not going to have yourself printed in this list (as you can see, this list does not have Vitaly). Alex: [swimming, video games, saxophone] Amy: [art, gaming, guitar] Daisey: [video games, guitar, cleaning, drawing, animated shows, reading, shopping] Daniel: [video games, tennis, soccer, biking, trumpet] Derek: [cooking, flute, gaming, swimming] Emily: [crochet, drawing, gardening, tuba, violin] Hudson: [anime, video games, trumpet] Jack: [cooking, swimming, music] Lily: [flute, ocarina, video games, baking] Matt: [piano, Reading, video games, traveling] Maya: [violin, knitting, reading, billiards] Nolan: [gym, piano, reading, video games] Ray: [piano, video games, volleyball] Roman: [piano, dancing, art] Sarah: [baking, reading, singing, theatre] Sean: [cooking, guitar, rainbow six] Sophie: [viola, ukulele, piano, video games] Stella: [roller skating, sudoku, watching baseball, harp] Teddy: [chess, lifting, swimming] Step 3. Find all students who share the same hobby with you and print them all on the screen Finally, your program should print the information related to the students who share the same hobby as you. In my case, it would be the following based on the above-mentioned file. There are 0 students sharing the same hobby called "hacking" with me. There are 1 students (Teddy) sharing the same hobby called "chess" with me. There are 2 students (Amy, Derek) sharing the same hobby called "table tennis" with me.

最新推荐

recommend-type

在Java中将List转换为String输出过程解析

Java中将List转换为String输出过程解析 在Java编程语言中,将List转换为String输出是一种常见的操作。这种操作可以将List中的元素转换为一个字符串,方便输出和显示。本文将详细介绍Java中将List转换为String输出的...
recommend-type

Java中实现String.padLeft和String.padRight的示例

public static String padWhitespaceLeft(String s, int len) { return String.format("%1$" + len + "s", s); } public static String padWhitespaceRight(String s, int len) { return String.format("%1$-" + ...
recommend-type

Java中Object.equals和String.equals的区别详解

Java中Object.equals和String.equals的区别详解 Java中的Object.equals和String.equals是两个不同的equals方法,它们之间的区别是非常重要的,理解这两个方法的区别对于我们编写高质量的Java代码非常重要。 首先,...
recommend-type

Android WebView支持input file启用相机/选取照片功能

Android WebView支持input file启用相机/选取照片功能 Android WebView支持input file启用相机/选取照片功能是指在Android应用程序中使用WebView组件时,如何启用input file的相机/选取照片功能。本文将通过实例...
recommend-type

三步搞定:Vue.js调用Android原生操作

public void callAndroid(final String msg) { deliver.post(new Runnable() { @Override public void run() { Log.i("Info", "main Thread:" + Thread.currentThread()); Toast.makeText(context....
recommend-type

利用迪杰斯特拉算法的全国交通咨询系统设计与实现

全国交通咨询模拟系统是一个基于互联网的应用程序,旨在提供实时的交通咨询服务,帮助用户找到花费最少时间和金钱的交通路线。系统主要功能包括需求分析、个人工作管理、概要设计以及源程序实现。 首先,在需求分析阶段,系统明确了解用户的需求,可能是针对长途旅行、通勤或日常出行,用户可能关心的是时间效率和成本效益。这个阶段对系统的功能、性能指标以及用户界面有明确的定义。 概要设计部分详细地阐述了系统的流程。主程序流程图展示了程序的基本结构,从开始到结束的整体运行流程,包括用户输入起始和终止城市名称,系统查找路径并显示结果等步骤。创建图算法流程图则关注于核心算法——迪杰斯特拉算法的应用,该算法用于计算从一个节点到所有其他节点的最短路径,对于求解交通咨询问题至关重要。 具体到源程序,设计者实现了输入城市名称的功能,通过 LocateVex 函数查找图中的城市节点,如果城市不存在,则给出提示。咨询钱最少模块图是针对用户查询花费最少的交通方式,通过 LeastMoneyPath 和 print_Money 函数来计算并输出路径及其费用。这些函数的设计体现了算法的核心逻辑,如初始化每条路径的距离为最大值,然后通过循环更新路径直到找到最短路径。 在设计和调试分析阶段,开发者对源代码进行了严谨的测试,确保算法的正确性和性能。程序的执行过程中,会进行错误处理和异常检测,以保证用户获得准确的信息。 程序设计体会部分,可能包含了作者在开发过程中的心得,比如对迪杰斯特拉算法的理解,如何优化代码以提高运行效率,以及如何平衡用户体验与性能的关系。此外,可能还讨论了在实际应用中遇到的问题以及解决策略。 全国交通咨询模拟系统是一个结合了数据结构(如图和路径)以及优化算法(迪杰斯特拉)的实用工具,旨在通过互联网为用户提供便捷、高效的交通咨询服务。它的设计不仅体现了技术实现,也充分考虑了用户需求和实际应用场景中的复杂性。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】基于TensorFlow的卷积神经网络图像识别项目

![【实战演练】基于TensorFlow的卷积神经网络图像识别项目](https://img-blog.csdnimg.cn/20200419235252200.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MTQ4OTQw,size_16,color_FFFFFF,t_70) # 1. TensorFlow简介** TensorFlow是一个开源的机器学习库,用于构建和训练机器学习模型。它由谷歌开发,广泛应用于自然语言
recommend-type

CD40110工作原理

CD40110是一种双四线双向译码器,它的工作原理基于逻辑编码和译码技术。它将输入的二进制代码(一般为4位)转换成对应的输出信号,可以控制多达16个输出线中的任意一条。以下是CD40110的主要工作步骤: 1. **输入与编码**: CD40110的输入端有A3-A0四个引脚,每个引脚对应一个二进制位。当你给这些引脚提供不同的逻辑电平(高或低),就形成一个四位的输入编码。 2. **内部逻辑处理**: 内部有一个编码逻辑电路,根据输入的四位二进制代码决定哪个输出线应该导通(高电平)或保持低电平(断开)。 3. **输出**: 输出端Y7-Y0有16个,它们分别与输入的编码相对应。当特定的
recommend-type

全国交通咨询系统C++实现源码解析

"全国交通咨询系统C++代码.pdf是一个C++编程实现的交通咨询系统,主要功能是查询全国范围内的交通线路信息。该系统由JUNE于2011年6月11日编写,使用了C++标准库,包括iostream、stdio.h、windows.h和string.h等头文件。代码中定义了多个数据结构,如CityType、TrafficNode和VNode,用于存储城市、交通班次和线路信息。系统中包含城市节点、交通节点和路径节点的定义,以及相关的数据成员,如城市名称、班次、起止时间和票价。" 在这份C++代码中,核心的知识点包括: 1. **数据结构设计**: - 定义了`CityType`为short int类型,用于表示城市节点。 - `TrafficNodeDat`结构体用于存储交通班次信息,包括班次名称(`name`)、起止时间(原本注释掉了`StartTime`和`StopTime`)、运行时间(`Time`)、目的地城市编号(`EndCity`)和票价(`Cost`)。 - `VNodeDat`结构体代表城市节点,包含了城市编号(`city`)、火车班次数(`TrainNum`)、航班班次数(`FlightNum`)以及两个`TrafficNodeDat`数组,分别用于存储火车和航班信息。 - `PNodeDat`结构体则用于表示路径中的一个节点,包含城市编号(`City`)和交通班次号(`TraNo`)。 2. **数组和变量声明**: - `CityName`数组用于存储每个城市的名称,按城市编号进行索引。 - `CityNum`用于记录城市的数量。 - `AdjList`数组存储各个城市的线路信息,下标对应城市编号。 3. **算法与功能**: - 系统可能实现了Dijkstra算法或类似算法来寻找最短路径,因为有`MinTime`和`StartTime`变量,这些通常与路径规划算法有关。 - `curPath`可能用于存储当前路径的信息。 - `SeekCity`函数可能是用来查找特定城市的函数,其参数是一个城市名称。 4. **编程语言特性**: - 使用了`#define`预处理器指令来设置常量,如城市节点的最大数量(`MAX_VERTEX_NUM`)、字符串的最大长度(`MAX_STRING_NUM`)和交通班次的最大数量(`MAX_TRAFFIC_NUM`)。 - `using namespace std`导入标准命名空间,方便使用iostream库中的输入输出操作。 5. **编程实践**: - 代码的日期和作者注释显示了良好的编程习惯,这对于代码维护和团队合作非常重要。 - 结构体的设计使得数据组织有序,方便查询和操作。 这个C++代码实现了全国交通咨询系统的核心功能,涉及城市节点管理、交通班次存储和查询,以及可能的路径规划算法。通过这些数据结构和算法,用户可以查询不同城市间的交通信息,并获取最优路径建议。