473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Java Arraylist Help

Hey guys, first time posting here so don't flame me for being new, I was just wondering if anyone could explain why my Arraylists are overwriting themselves.
Expand|Select|Wrap|Line Numbers
  1.     public static void returnToBook(entry x){
  2.         addressBook.add(persevreIndex, x);
  3.         reversedBook.add(persevreIndex, x);
  4.         phoneBook.add(persevreIndex, x);
  5.         tempEntry = (entry) addressBook.get(persevreIndex);
  6.         temp = tempEntry.getName();
  7.         entryList.addElement(temp);
  8.         reversedList.addElement(temp);
  9.         phoneList.addElement(tempEntry.getPhoneNumber());
  10.         refreshzor();
  11.         revered();
  12.         phoneSort();
  13.     }
If you need more of the code please tell me, the bad news for me is that the zipped file containing all of my code is just to big to fit. I built the code in netbeans btw
Feb 23 '09 #1
8 4037
chaarmann
785 Expert 512MB
As I can see from the code you have provided, all is ok, you have added the entry. But maybe you are deleting the elements later on? Please provide code for "refreshzor()" and other functions you are calling later on.

Also it would be nice to have the code where the ArrayLists phoneBook etc. are defined. Maybe you are overwriting the reference to them somewhere?

And most important: put code statements everywhere in your code that print out the size of the ArrayLists. Then we can see where the elements are dereferenced/deleted.
Feb 24 '09 #2
Sorry about not being able to post the code earlier, was at school and can't use mediafire there, anyway here is the whole project, open the Ad dreesBookView and NewEntry classes in netbeans, those are the gui's and then the class I made myself for storing the entries is the entry class in the resources folder.

link: http://www.mediafire.com/

P.S. Sorry about how messy the code is, I am still learning how to program and this project is for my AP computer science class.
Feb 24 '09 #3
JosAH
11,448 Expert 8TB
@royalaid
That is not the way to ask a question; post an sscce instead.

kind regards,

Jos
Feb 24 '09 #4
Honestly there is not much I can chop off that isn't going to affect the outcome, if you want I can highlight what i think the problem is but I don't know where it is coming from so that is why I posted all of the code, I am sorry if that is problem but I am still learning and not sure where or what to post. Also I switched the code so that it would store using a Array instead of a Arraylist, no change in the outcome of the results.

Refreshzor Method:
Expand|Select|Wrap|Line Numbers
  1.     public static void refreshzor(){
  2.         Collections.sort(addressBook , new Comparator()
  3.         {
  4.         public int compare(Object o1, Object o2){
  5.         entry e1 = (entry) o1;
  6.         entry e2 = (entry) o2;
  7.         return e1.getName().compareToIgnoreCase(e2.getName());
  8.         }});
  9.         entryList.removeAllElements();
  10.         for (int x =0; x < addressBook.size(); x++){
  11.             tempEntry = (entry) addressBook.get(x);
  12.             temp = tempEntry.getName();
  13.             entryList.addElement(temp);
  14.         }
  15.     }
phoneSort and revereSort are Identical to this except that they use phoneBook and reverseBook.

Also the code used to add the enties:
Expand|Select|Wrap|Line Numbers
  1.     private void addContact(java.awt.event.MouseEvent evt) {                            
  2.         temp = nameField.getText();
  3.         NoName NoName;
  4.  
  5.         if(temp.equalsIgnoreCase("")){
  6.              JFrame mainFrame = AddressBookApp.getApplication().getMainFrame();
  7.              NoName = new NoName(mainFrame, true);
  8.              NoName.setLocationRelativeTo(mainFrame);
  9.              AddressBookApp.getApplication().show(NoName);}
  10.         else
  11.         newEntry.setName(temp);
  12.  
  13.         temp = nickField.getText();
  14.         if(temp.equalsIgnoreCase("")){}
  15.         else
  16.         newEntry.setNick(temp);
  17.  
  18.         temp = cityField.getText();
  19.         if(temp.equalsIgnoreCase("")){}
  20.         else
  21.         newEntry.setCity(temp);
  22.  
  23.         temp = stateField.toString();
  24.         if(temp.equalsIgnoreCase(" ")){}
  25.         else
  26.         newEntry.setState(temp, stateField.getSelectedIndex());
  27.  
  28.         temp = addressField.getText();
  29.         if(temp.equalsIgnoreCase("")){}
  30.         else
  31.         newEntry.setAddress(temp);
  32.  
  33.         temp = emailField.getText();
  34.         if(temp.equalsIgnoreCase("")){}
  35.         else{
  36.             if (temp.indexOf('@') < 0 || temp.indexOf('.') < 0){
  37.               JFrame mainFrame = AddressBookApp.getApplication().getMainFrame();
  38.               UshallNotEmailFail = new UshallNotEmailFail(mainFrame, true);
  39.               UshallNotEmailFail.setLocationRelativeTo(mainFrame);
  40.               AddressBookApp.getApplication().show(UshallNotEmailFail);
  41.             }
  42.             else{
  43.                 newEntry.setEmail(temp);
  44.             }
  45.         }
  46.  
  47.         temp = zipCodeField.getText();
  48.         if(temp.equalsIgnoreCase("     ")){}
  49.         else
  50.         newEntry.setZip(temp);
  51.  
  52.         temp = phoneNumberField.getText();
  53.         if(temp.equalsIgnoreCase("(   )    -    ")){}
  54.         else
  55.         newEntry.setPhone(temp);
  56.  
  57.         temp = birthDayField.getText();
  58.         if (temp.equals("  /  /    ")){}
  59.         else
  60.         newEntry.setBirth(temp);
  61.  
  62.         /*if (AddressBookView.editSwitchStatus() == true){
  63.         AddressBookView.returnToBook(newEntry);
  64.         AddressBookView.editSwitchOff();
  65.         }
  66.         else*/
  67.         AddressBookView.addToBook(newEntry);
  68.  
  69.  
  70.         temp = newEntry.getName();
  71.         if(temp.equalsIgnoreCase("")){}
  72.         else
  73.             setVisible(false);
Finally the entire entry class:
Expand|Select|Wrap|Line Numbers
  1. public class entry{
  2.     protected String name;
  3.     protected String nickName;
  4.     protected String city;
  5.     protected String state;
  6.     protected int    stateIndex;
  7.     protected String address;
  8.     protected String zipCode;
  9.     protected String email;
  10.     protected String phoneNumber;
  11.     protected String birthyear;
  12.     protected String birthmonth;
  13.     protected String birthday;
  14.  
  15.     public entry(String n,String nN, String bD, String a, String c, String s, int sI, String zC, String e, String pN) {
  16.         name = n;
  17.         nickName = nN;
  18.         city = c;
  19.         state = s;
  20.         stateIndex = sI;
  21.         address = a;
  22.         zipCode = zC;
  23.         email = e;
  24.         phoneNumber = pN;
  25.         birthday = bD;
  26.     }
  27.  
  28.     public entry(){
  29.         name = "";
  30.         nickName = "";
  31.         city = "";
  32.         state = "";
  33.         stateIndex = 0;
  34.         address = "";
  35.         zipCode = "";
  36.         email = "";
  37.         phoneNumber = "";
  38.         birthday = "";
  39.     }
  40.  
  41.     public void setName(String n){
  42.         name = n;
  43.     }
  44.  
  45.     public void setNick(String nN){
  46.        nickName = nN;
  47.     }
  48.  
  49.     public void setBirth(String bD){
  50.         birthday = bD;
  51.     }
  52.  
  53.     public void setAddress(String a){
  54.         address = a;
  55.     }
  56.  
  57.     public void setCity(String c){
  58.         city = c;
  59.     }
  60.  
  61.     public void setState(String s, int sI){
  62.         state = s;
  63.         stateIndex = sI;
  64.     }
  65.  
  66.     public void setZip(String zC){
  67.         zipCode = zC;
  68.     }
  69.  
  70.     public void setPhone(String pN){
  71.         phoneNumber = pN;
  72.     }
  73.  
  74.     public void setEmail(String e){
  75.         email = e;
  76.     }
  77.  
  78.     public String getName(){
  79.         return name;
  80.     }
  81.  
  82.     public String getAddress(){
  83.         return address;
  84.     }
  85.  
  86.     public String getCity(){
  87.         return city;
  88.     }
  89.  
  90.     public String getState(){
  91.         return state;
  92.     }
  93.  
  94.     public int getStateIndex(){
  95.         return stateIndex;
  96.     }
  97.  
  98.     public String getZipCode(){
  99.         return zipCode;
  100.     }
  101.  
  102.     public String getPhoneNumber(){
  103.         return phoneNumber;
  104.     }
  105.  
  106.     String getBirth() {
  107.         return birthday;
  108.     }
  109.  
  110.     String getNick(){
  111.         return nickName;
  112.     }
  113.  
  114.     String getEmail(){
  115.         return email;
  116.     }
  117. }
Which is just a basic storage class
Feb 24 '09 #5
chaarmann
785 Expert 512MB
The problem is that you have global variables like "addressBook", "entryList" etc. that can affect each other everywhere. That's a bad programming practice and leads to the problem that you lose the overview where which variable is manipulated. Try to make your methods free of global variables, use only local ones. For example in your method "refreshzor", you are manipulating the global variable "entryList". First you add an element to the list in method "returnToBook" with "entryList.addElement(temp);". Then you call method "refreshzor", where you delete all elements of "entryList" with "entryList.removeAllElements(); ". So why add it first if you delete it right away? That's what I mean with side effects that global variables have. Try to think more object-orientated. What is the input, what is the output? A method like "refreshzor()" says nothing. But if I write "addressBook.sort(); tempEntry=copy(addressBook);" everybody understands: the object "addressBook" is sorted and then a copy is made and stored in tempEntry. And most important: no side effects. I can see directly which objects are manipulated.

If that's too much work for you, here is an example of what I mean for minimum changes:

Expand|Select|Wrap|Line Numbers
  1.     public static void returnToBook(entry x){
  2.         addressBook.add(persevreIndex, x);
  3.         reversedBook.add(persevreIndex, x);
  4.         phoneBook.add(persevreIndex, x);
  5.         tempEntry = (entry) addressBook.get(persevreIndex);
  6.         temp = tempEntry.getName();
  7.         // entryList.addElement(temp); // commented out, because no effect
  8.         reversedList.addElement(temp);
  9.         phoneList.addElement(tempEntry.getPhoneNumber());
  10.         // refreshzor(); // old, deprecated function, do not use
  11.         entryList = refreshzor(addressBook); // here I can see that addressBook and entryList are changed.
  12.         revered();
  13.         phoneSort();
  14.     }
and your global-variables-free method:
Expand|Select|Wrap|Line Numbers
  1.    public static ArrayList refreshzor(ArrayList addressBook){ 
  2.         Collections.sort(addressBook , new Comparator() 
  3.         { 
  4.         public int compare(Object o1, Object o2){ 
  5.         entry e1 = (entry) o1; 
  6.         entry e2 = (entry) o2; 
  7.         return e1.getName().compareToIgnoreCase(e2.getName()); 
  8.         }}); 
  9.         ArrayList entryList = new ArrayList(); 
  10.         for (int x =0; x < addressBook.size(); x++){ 
  11.             tempEntry = (entry) addressBook.get(x); 
  12.             temp = tempEntry.getName(); 
  13.             entryList.addElement(temp); 
  14.         } 
  15.         return entryList;
  16.     } 
  17.  
So, you have still not yet listed the code where the problem could be.
The code for functions "revered" and "phoneSort", that are called after "refreshzor", is still missing. Maybe you are manipulating global object again there, like "phoneList.removeAllElements()" or so ?. Also the code where "returnToBook" is called is missing, please list it here. It may be needed in the case that the analysis shows that your entries are not magically deleted inside your "returnToBook" function, that means the problem lies somewhere else outside.

To check for that I asked you kindly in my last forum posting to add code for printing out the list sizes and publish the output in your next posting. But you didn't do. Can you do it now, please? It would be a big help to detect the error!!! What I mean is, change your method "returnToBook" to:


Expand|Select|Wrap|Line Numbers
  1.     public static void returnToBook(entry x){
  2.         addressBook.add(persevreIndex, x);
  3.         reversedBook.add(persevreIndex, x);
  4.         phoneBook.add(persevreIndex, x);
  5.         tempEntry = (entry) addressBook.get(persevreIndex);
  6.         temp = tempEntry.getName();
  7.         entryList.addElement(temp); 
  8.         reversedList.addElement(temp);
  9.         phoneList.addElement(tempEntry.getPhoneNumber());
  10.         System.out.println("Step 1: size of addressBook=" + addressBook.size());
  11.         refreshzor(); 
  12.         System.out.println("Step 2: size of addressBook=" + addressBook.size());
  13.         revered();
  14.         System.out.println("Step 3: size of addressBook=" + addressBook.size());
  15.         phoneSort();
  16.         System.out.println("Step 4: size of addressBook=" + addressBook.size());
  17.     }
Of course you should add more statements the same way to also list the sizes for the other lists you have (that means for "reversedBook", "phoneBook", "reversedList", "entryList" and "phoneList")
Feb 25 '09 #6
JosAH
11,448 Expert 8TB
@OP: also: perform some unit tests; call your methods in isolation and check whether or not each method does what it's supposed to do; showing us a lot of juggling with some ArrayLists and asking "what goes wrong?" doesn't bring you any help.

kind regards,

Jos
Feb 25 '09 #7
Ok I am going to try both of your suggestions, hopefully this will get me somewhere, and sorry about not posting the sizes of the arrays totally missed the fact you asked for that in your last post, makes me feel like more of a idiot. I should have that code up really soon if my teacher lets me work on it.
Feb 25 '09 #8
I figured out the problem, I wasn't instantiating a new entry when I added the contact so effectively my array was one giant pointer... anyway thanks for the help guys
Feb 25 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: daniel | last post by:
I am working with a project which converts a java based application into C#. I noticed that when I converted the java file, .net throws a SupportClass for me and since there is no List type in C#,...
0
by: daniel li | last post by:
I got another convert issue. This is the same project as I posted yesterday, and received a perfect response. The conversion threw a SupportClass for me and one of the data type is...
19
by: Xandau | last post by:
hello all, i wotk with java every day but last time i have interested in C#. everything goes great except one thing... in java everything is a reference (except plain types) so i thought that...
13
by: Andrew Bell | last post by:
I'm doing a uni course in the UK and one of my semesters is about programming. This is just going to be compilied and executed with no menu just using command promt (javac classfile.class) I am...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: VeeraLakshmi | last post by:
I am doing a project for internet control using Java,PHP and MySql.All sites should go through the proxy server only.We are giving access rights as allow or deny to the sites.If we type the...
2
by: chokcheese | last post by:
I'm having trouble with a java application. When I try and run the program it shows a java.lang.NullPointerException in the "tic.getClient().getClientTicketList().add(tic);" line (it's in bold). I...
20
by: cowboyrocks2009 | last post by:
Hi, I need help to automate my code to take data from input file. Also I need to create it as a function so that I can pass it to some other program. I am new to Java so having a bit limitation to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.