fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5.  
  6. class MyList<T>{
  7. private class Node{
  8. public int No;
  9. public T Value;
  10. public Node Next;
  11.  
  12. public Node(T value){
  13. this.No = 0;
  14. this.Value = value;
  15. this.Next = null;
  16. }
  17. }
  18.  
  19. private int _count;
  20. private Node _root;
  21.  
  22. public MyList(){
  23. this._count = 0;
  24. this._root = null;
  25. }
  26.  
  27. public void Add(T value){
  28. Node newNode = new Node(value);
  29. newNode.No = this._count;
  30.  
  31. if(this._root != null){
  32. newNode.Next = this._root;
  33. this._root = newNode;
  34. }
  35. else this._root = newNode;
  36. this._count += 1;
  37. }
  38.  
  39. public T GetItem(int No){
  40. Node temp = this._root;
  41. while(temp != null){
  42. if(temp.No == No) return temp.Value;
  43. temp = temp.Next;
  44. }
  45. return null;
  46. }
  47. }
  48.  
  49. class Person{
  50. public String Name;
  51. public String Surname;
  52. public Person(String name, String surname){
  53. this.Name = name;
  54. this.Surname = surname;
  55. }
  56. }
  57.  
  58. class Ideone
  59. {
  60. public static void main (String[] args) throws java.lang.Exception
  61. {
  62. MyList<Person> persons = new MyList<Person>();
  63. persons.Add(new Person("Tomek", "Tomkowski"));
  64. persons.Add(new Person("Karolina", "Karolinowska"));
  65. persons.Add(new Person("Kasia", "Kasiowska"));
  66. persons.Add(new Person("Maciek", "Maciowski"));
  67.  
  68. Person selected = persons.GetItem(2);
  69. System.out.println(selected.Name + " " + selected.Surname);
  70. }
  71. }
Success #stdin #stdout 0.11s 320576KB
stdin
Standard input is empty
stdout
Kasia Kasiowska