-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayList.java
145 lines (121 loc) · 4.49 KB
/
ArrayList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Java Program to create an ArrayList with multiple Object Types using Student Object.
import java.util.ArrayList;
public class ArrayObject{
public static void main(String[] args){
ArrayList <Student> studentInfo = new ArrayList<>();
studentInfo.add(new Student(101, "Chandana", "XYZ"));
studentInfo.add(new Student(102, "Tina", "ABC"));
studentInfo.add(new Student(103, "Jaggu", "PQR"));
studentInfo.add(new Student(104, "Aakash", "RST"));
for(Student student : studentInfo){
System.out.println("Roll Number : " + student.stuRollNum + ", Name : " + student.stuName + ", Address : " + student.stuAddress);
}
}
}
class Student{
int stuRollNum;
String stuName;
String stuAddress;
Student(int stuRollNum, String stuName, String stuAddress){
this.stuRollNum = stuRollNum;
this.stuName = stuName;
this.stuAddress = stuAddress;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
/*RollNo Name Age Marks
101 Chandana 18 90
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
Write the above data in a text file, write a Java Program to read from the text file and create a Student Object from each line.
Now use an ArrayList to add Student Objects created above which means add each Stdent Object(which corresponds to a line) into the ArrayList.*/
// 1st : Using the normal String Data type
import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
public class StudentObject_1{
class Student{
int rollNumber;
String name;
int age;
double marks;
Student(int rollNumber, String name, int age, double marks){
this.rollNumber = rollNumber;
this.name = name;
this.age = age;
this.marks = marks;
}
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new FileReader("File Path"));
StringBuffer sb = new StringBuffer();
String line;
while((line = br.readLine()) != null){
sb.append(line);
sb.append("\n");
String comma = ", ";
int Start = 0, end;
end = line.indexOf(comma, Start);
while(end != -1){
String split = line.substring(Start, end);
Start = end + 1;
end = line.indexOf(comma, Start);
String Split = line.substring(Start);
}
String[] elements = line.split(", ");
List<String> list = Arrays.asList(elemnets);
ArrayList<String> listOfString = new ArrayList<String>(list);
System.out.println(listOfString);
}
br.close();
}
}
// 2nd : Using Student Object
import java.util.List;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
public class StudentObject_2 {
public static void main(String[] args){
BufferedReader br = new BufferedReader(new FileReader("File Path"));
StringBuffer sb = new StringBuffer();
String line;
while((line = br.readLine()) != null){
sb.append(line);
sb.append("\n");
String[] elements = line.split(", ");
int rollNo = Integer.parseInt(elements[0].trim());
String name = elements[1];
int age = Integer.parseInt(elements[2].trim());
Double marks = Double.parseDouble(elements[3].trim());
Student stu = new Student(rollNo, name, age, marks);
studentList.add(stu);
}
br.close();
for(Student info : studentList){
System.out.println(info);
}
}
}
// Student Class for StudentObject_2 Class
public class Student{
int rollNumber;
String name;
int age;
double marks;
Student(int rollNumber, String name, int age, double marks){
this.rollNumber = rollNumber;
this.name = name;
this.age = age;
this.marks = marks;
}
public String toString(){
return "Roll Number : " + rollnumber + "Name : " + name + "Age : " + age + "Marks : " + marks;
}
}