arraylist - Java Collection not working, Help needed -


i working on beginners project learning java. project add, list , delete person.

type 1 add person type 2 list person type 3 delete person.

i using arraylist collections work on project.

the first question is using collections aproach? if yes declaring arraylist globally while iterating can see it's not storing data.

my code follows.

import java.util.*;    public class operationclass implements operationinterface {       string id;       string fname;       string lname;       arraylist<string> person=new arraylist<string>();        public static void main(string args[]) {           new operationclass().operation();       }        public static void operation() {           int a;           scanner in = new scanner(system.in);           system.out.println("enter 1 adding new record");           system.out.println("enter 2 see list of records");           system.out.println("enter 3 delete record");           system.out.println("enter 4 exit!!");            = in.nextint();            if (a == 1) {               new operationclass().addperson();           } if (a == 2) {               new operationclass().listperson();           } if (a == 3) {               new operationclass().deleteperson();           } if (a == 4) {               system.out.println("thanks using program");           }       }        /**       * adds new person , stores in collection       */       @override       public void addperson() {             scanner in = new scanner(system.in);   new operationclass().operation();           system.out.println("enter id");           id = in.next();            system.out.println("enter first name");           fname = in.next();            system.out.println("enter last name");           lname = in.next();             person.add(id);           person.add(fname);           person.add(lname);              new operationclass().operation();       }        @override       public void listperson() {           iterator itr=person.iterator();           while(itr.hasnext()){               system.out.println(itr.next());           }        new operationclass().operation();        }        @override       public void deleteperson() {           system.out.println("delete under construction");       }        @override       public void quit() {        }   }   

first output is

enter 1 adding new record.

enter 2 see list of records.

enter 3 delete record .

enter 4 exit!!

while entering 1 coming this

enter 1 adding new record

enter 2 see list of records

enter 3 delete record

enter 4 exit!!

1

enter id

1

enter first name

indranil

enter last name

sinha

then after hitting enter again first 4 options coming , after going 2 not showing anything.

enter 1 adding new record

enter 2 see list of records

enter 3 delete record

enter 4 exit!!

1

enter id

1

enter first name

asdasd

enter last name

dsffdgdfg

enter 1 adding new record

enter 2 see list of records

enter 3 delete record

enter 4 exit!!

2

enter 1 adding new record

enter 2 see list of records

enter 3 delete record

enter 4 exit!!

you have declared arraylist instance variable.

in simplest words means a new object new arraylist cretaed.

now while calling methods doing, new operationclass().addperson();

in these statements new operationclass(); creates new object , hence creates new arraylist.

therefore in last don't in 1 list.

i suggest following:

  1. as public static void operation() method static, don't need create object call it. can call directly class name.

therefore new operationclass().operation(); can replaced operationclass.operation();. won't solve problem.

solutions

  1. what need have 1 common object of operationclass , store in reference variable use 1 object call other methods i.e. don't call new operationclass() again , again.

  2. the alternate can work same code declare arraylist static i.e. static arraylist<string> person=new arraylist<string>();.

by doing common objects , adding data single common arraylist.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -