-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.java
More file actions
64 lines (59 loc) · 4.4 KB
/
Copy pathData.java
File metadata and controls
64 lines (59 loc) · 4.4 KB
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
// Obj-List-Example
import java.util.*;
class Model
{
String Name;
String Email;
Long phone;
public Model (String Name,String Email,Long phone)
{
this.Name=Name;
this.Email=Email;
this.phone=phone;
}
@Override
public String toString()
{
return this.Name + "," + this.Email + "," + this.phone;
}
}
public class Data
{
public static void main (String [] args)
{
List<Model> list = new ArrayList<>();
System.out.println("Before adding the data to the list ");
System.out.println("Size of the list=" +list.size());
System.out.println("List data=" + list);
String Name = "GitHub";
String Email = "github_39@gmail.com";
Long phone = Long.valueOf (String.valueOf (1234567890));
list.add(new Model(Name,Email,phone));
System.out.println("\nAfter adding data to the list.....");
System.out.println("Size of list=" + list.size());
System.out.println("List data=" + list.get(0).toString());
}
}
/*
| **Part / Line** | **Code Snippet** | **Explanation** |
| --------------- | ----------------------------------------------------------- | --------------------------------------------------------------------------------- |
| 1 | `import java.util.*;` | Imports all utility classes from `java.util` package (e.g., `List`, `ArrayList`). |
| 2 | `class Model` | Defines a custom class named `Model` to store data (Name, Email, Phone). |
| 3 | `String Name; String Email; Long phone;` | Declares three instance variables for storing model data. |
| 4 | `public Model(String Name, String Email, Long phone)` | Constructor — used to initialize object values when created. |
| 5 | `this.Name = Name; this.Email = Email; this.phone = phone;` | `this` keyword assigns constructor parameters to instance variables. |
| 6 | `@Override public String toString()` | Overrides the default `toString()` method to define how the object is printed. |
| 7 | `return this.Name + "," + this.Email + "," + this.phone;` | Returns a string combining all the field values for display. |
| 8 | `public class Data` | Main class containing the `main()` method to execute the program. |
| 9 | `List<Model> list = new ArrayList<>();` | Creates an empty `ArrayList` to store `Model` objects. |
| 10 | `System.out.println("Before adding the data...");` | Prints initial information before adding data. |
| 11 | `list.size()` | Returns number of elements in the list (initially `0`). |
| 12 | `list` | Displays the list contents (initially empty `[]`). |
| 13 | `String Name = "GitHub";` | Declares and initializes a variable for name. |
| 14 | `String Email = "github_39@gmail.com";` | Declares and initializes a variable for email. |
| 15 | `Long phone = Long.valueOf(String.valueOf(1234567890));` | Converts a numeric value to `Long` type. |
| 16 | `list.add(new Model(Name, Email, phone));` | Creates a new `Model` object and adds it to the list. |
| 17 | `System.out.println("After adding data...");` | Prints message after adding object. |
| 18 | `list.size()` | Displays updated list size (now `1`). |
| 19 | `list.get(0).toString()` | Accesses the first object and displays its details using the `toString()` method. |
*/