Array of Linked List Help

I am fairly new to Java, and Linked Lists. Can you tell me how I can fix my code? Why does my code print infinite "3" s. That is all I am trying to do is create a linked list and put that linked list into index 2 of the array. In the loop, I am setting up a whole bunch on null Node's, is it even possible to link all these nodes together in that same loop? If something does not make sense, I will try to elaborate. I will eventually impliment an insert method in the Node class, are there any pointers on how to implement it? What are the special cases I need to be looking for when implementing the insert method?
* To change this template, choose Tools | Templates
* and open the template in the editor.
package testlinkedlist;
* @author Ben
public class Main {
     * @param args the command line arguments
    public static Node l;
    public static Node p = new Node();
    public static Node Front;
    public static void main(String[] args) {
        Node[] anArray = new Node[4];
        for(int i = 0; i < 4; i++){
            anArray[i] = new Node();
            l = new Node(i);
            l.setLink(l);
        anArray[2] = l;         
        while(anArray[2] != null){
            System.out.println(anArray[2].dataInNode());
            anArray[2] = anArray[2].next;
}

l = new Node(i);
l.setLink(l);The node l is created in line one, in line two a link is created from l to l. In other words here is the presence of your infinite loop which is magnified in the following piece of code:
anArray[2] = l;         
while(anArray[2] != null){
  System.out.println(anArray[2].dataInNode());
  anArray[2] = anArray[2].next;
}Mel

Similar Messages

  • Sorting linked lists - Help

    I am writing a program for an assignment that reads a file containing population growth for all the Counties in the U.S. I'm required to use a link list to store the data. They want me to sort the data based on certain criteria. My first problem is that as each new County instance is created, it is to be inserted into this linked list in alphabetical order first by county name.
    My understanding of linked lists is that they cannot be randomly accessed like a vector or an array. So is it possible to do this without using a vector or an array? And how would I go about it?
    thanks.

    Can you create a second, sorted linked list?The prof. didn't specify whether or not I can use a second list.
    Are you prohibited from using the collections framework (probably, if it is a school assignment!)Not really sure on this one. Again it is not specified in the assignment
    (Why would they have you store this data in a linked list??)I their reasoning is to have us learn how to implement linked list because it can grow or shrink when necessary.(Other than that I don't understand the practicality of it either)
    Are you using a doubly linked list (forwards and backwards)?The assignment does not specify a certain linked list to use.
    Did your prof mention any sort algorithms you might use? He states later in the assignment that I have to generate different growth reports by using MergeSort.
    I appreciate your help and comments. Unfortunately my prof. is very vague about the assignment and its implementation in his outline. He just kind of throws us into it hoping that we will figure it out.

  • Alphabetizing a linked list, HELP!

    To all:
    Firstly, I'm not looking for someone to do my assignment for me. I just need a push in the right direction...
    I need to create a linked list that contains nodes of a single string (a first name), but that list needs to be alphabetized and that's where I am stuck.
    I can use an iterator method that will display a plain-old "make a new node the head" linked list with no problem. But I'm under the impression that an iterator method is also the way to go in alphabetizing this list (that is using an iterative method to determine the correct place to place a new node in the linked list). And I am having zero luck doing that.
    The iterator method looks something like this:
    // ---- MyList: DisplayList method ----
    void DisplayList()
    ListNode p;
    for (ResetIterator(); (p = Iterate()) != null; )
    p.DisplayNode();
    // ---- MyList: ResetIterator method ----
    void ResetIterator()
    current = head;
    // ---- MyList: Iterate method ----
    ListNode Iterate()
    if (current != null)
    current = current.next;
    return current;
    Can I use the same iterator method to both display the final linked list AND alphabetize my linked list or do I need to create a whole new iterator method, one for each?
    Please, someone give me a hint where to go with this. I've spent something like 15 hours so far trying to figure this thing out and I'm just stuck.
    Thanks so much,
    John
    [email protected]

    I'll try and point you in the right direction without being too explicit as you request.
    Is your "linked list" an instance of the Java class java.util.LinkedList or a class of your own?
    If it is the Java class, then check out some of the other types of Collections that provide built-in sorting. You should be able to easily convert from your List to another type of Collection and back again to implement the sorting. (hint: you can do this in two lines of code).
    If this is your own class and you want to code the sort yourself, implement something simple such as a bubble sort (should be easy to research on the web).
    Converting to an array, sorting that and converting back is another option.
    Good Luck.

  • Circular Linked List Help

    Hi,
    I'm developing a circular linked list class.. However everytime I add to the front of the list it overwrites the first element in the list.. I pretty confused at the moment. If anyone could assist me in what I'm doing wrong it would be greatly appreciated..
    My Class:
    public class CNode<T>
         public T nodeValue;          //data held by the node
         public CNode<T> next;     //next node in the list
         //default constructor; next references node itself
         public CNode()
              nodeValue = null;
              next = this;
         //constructor; initializes nodeValue to item
         //and sets the next to reference the node itself
         public CNode(T item)
              nodeValue = item;
              next = this;
    }My addFirst:
    public static <T> void addFirst(CNode<T> header, T item)
              CNode<T> curr;
              CNode<T> newNode = new CNode<T>(item);
              //header.next = newNode;
              //newNode.next = header;
              curr = header;
              newNode.next = header;
              curr.next = newNode;
         }

    You need a Node class and a class that manages the nodes. The class the manages the nodes is typically called something like MyLinkedList. The MyLinkedList class will typically have a member called 'head' or 'first' and another member called 'last', 'end', or 'tail'. Those members will contain references to the first node and the last node respectively.
    The methods like add(), remove(), find(), etc. will be members of the MyLinkedList class--not the Node class. The add() method of the MyLinkedList class will create a node, set the value of the node, and then set the next member to refer to the proper node or null if the node is added to the end of the list. Therefore, there is really no need for a default constructor in the node class. The MyLinkedList class will always set the value of the node, and then set it's next member to refer to something.
    You might want to try to write a linear linked list first, and get that working, and then modify it to make it a circular linked list. In addition, you should be drawing pictures of nodes with arrows connecting the different nodes to understand what's going on. Whenver the next member of a node refers to another node, draw an arrow starting at the next member and ending at the node it refers to. The pictures will be especially helpful when you write functions like add().
    Message was edited by:
    7stud

  • Java Linked List Help

    Hey,
    I am having trouble understanding the Linked List implementation code given under the Node Operations part of this pdf.
    http://www.cs.berkeley.edu/~jrs/61b/lec/07.pdf
    public ListNode(int item, ListNode next) {
    this.item = item;
    this.next = next;
    public ListNode(int item) {
    this(item, null);
    ListNode l1 = new ListNode(7, new ListNode(0, new ListNode(6)));
    I understand the earlier implementation, but this one has me confused. Would someone please go through/explain the flow of this implementation with me?
    Thanks

    Well the code is pretty self explanitory.
    a ListNode class that has two fields. an int field item, and a ListNode field next.
    so when you create a ListNode object you can do so by one of two means passing one parameter or passing two parameters.
    if you pass one parameter then you have a ListNode which has an int say 1 and a ListNode object who's value is null.
    if you pass two parameters then you have a ListNode which has an int say 2 and a ListNode object who's value is the ListNode you pass to it.
    ex ListNode myNode = ListNode(1,new ListNode(2));
    so myNode now has an integer 1 and a reference to another ListNode which has an integer value of 2 and a reference to a null ListNode.
    hope this helps.

  • Linked List Help!

    Hi,
    I have an assignment on linked lists, as a beginner I am really struggling with it for hours...
    I have to write the following methods without importing any libraries.
    "Write an iterative method delete() for LinkedStackOfStrings that takes an integer parameter k and deletes the kth element (assuming it exists).
    Write a method find() that takes an instance of LinkedStackOfStrings and a string key as arguments and returns true if some node in the list has key as its item field, false otherwise."
    It must be implemented in the code on this site:
    [http://www.cs.princeton.edu/introcs/43stack/LinkedStackOfStrings.java.html]
    Help would be appreciated! I have been trying for hours :(

    vdL wrote:
    Thanks for the reply! Yes, I forgot add what my actual problem is.
    If I am correct, I will need to be able to traverse through the list up to a certain point, redirect and redirect the pointers.
    My problem is I am able to traverse through the list using 'for(Node x = first; x != null; x= x.next)' but I do not know how make it only run up to a particular position in the list.You can keep an integer counter, increment it each time through the loop, and break out of the loop when the counter is big enough. Example of breaking:
    if (n == k) break;
    And also, how not to loose the previous Node when traversing trough the list.You're allowed to have multiple references. So you could keep one reference to the current node, and one reference to the one before it.
    Or, rather than looping through the list until you find the kth element, loop until you find the (k - 1)th element, then (if it exists) remove the one after it.

  • Link list help

    Hi, I am implementing a graph for my Java project and part of the coding requires link list for the edges.
    I wrote the code, but I just don't understand why it does not function the way like it is in C++.
    Bascilly, I have trouble adding data into a linklist
    My codes are as follows:
    public int insertLink(String fCity, String tCity, int distance)
         int location = find(fCity);
         if(location == -1)
              return -1;
         else
         insertNode(new Link(tCity,distance), location);
         return 0;     
    private void insertNode(Link node, int index)
              Link head = vertex[index].head;
              while(head != null)
                   head = head.next;
              head = node;
    This is to add stuff to the list, but for some reason, when i try to display the data, i keep getting null. somehow when
    head = node
    head = new Link(data, data);
    the head points to somwhere else, and not refer to the linklist vertex[index].head anymore...
    Here is my code to display my data:
    public void printVertex()
         for(int i = 0; i < vertex.length; i++)
              if(vertex[i] != null)
                   System.out.print(vertex[i]+" --");
                   Link node = vertex.head;
                   while(node != null)
              System.out.print(node.toCity+","+node.distance+"--");
                             node = node.next;
                   System.out.println();
    Am i doing something wrong so my linklist isn't implement correct?
    THank you.

    Hi, the "fix" for head.next != null didn't work
    private void insertNode(Link node, int index)
              Link head = vertex[index].head;
              while(head != null)
                   head = head.next;
              head = node;
              System.out.println(head.toCity+","+head.distance);
              System.out.println(vertex[index].head.toCity+","+vertex[index].head.distance);
         }basiclly. When i do System.out.println(head.toCity+","+head.distance);, it can display data
    but System.out.println(vertex[index].head.toCity+","+vertex[index].head.distance);
    it throow null's error.
    Btw, yes , i know Java have build in Link list, but unfortunately, my professor likes to reinvent the wheel and want us to do everything from scratch...
    So any other help will be useful, thank you.

  • Linked lists help... how exactly is this a NullPointerException?

    So I have a linked list class with variables head and tail both initialized to "null", with variables for size, declare, and traveler (not really relevant to this though). We have to create methods to manipulate said list.
    For a test I declared an instance of this linked list:
    static SinglyLinkedList list = new SinglyLinkedList();One of the methods we have to create is an append method, here's the code for that:
    public void append(Object dataToAdd)
              if(head == null & tail == null)
                   head = new Node(dataToAdd, null);
                   tail = head;
              else
                   tail.next = new Node(dataToAdd, null);
                   tail = tail.next;
         }Testing it with...
    list.append("A");Gives null pointer exceptions at the if statement in the append method as well as at the list.append line. But why? What is exactly pointing to null?

    Ah... I didn't think of head not being null but tail being null.
    I initiated the traveler node in the linked list class to head.
    then I added this in between the if and else statements I had:
    else if(head != null & tail == null)
                   while(traveler.next != null)
                        traveler = traveler.next;
                   tail = traveler;
                   tail.next = new Node(dataToAdd, null);
                   tail = tail.next;
                            }But it's still giving me the same errors.
    Edited by: klawson88 on Feb 12, 2009 12:32 AM

  • Array of linked list

    I have the following:
              LinkedList pathArray[] = new LinkedList[20];
                            for(int i = 0; i < pathArray.length; i++)
                   pathArray[i] = new LinkedList();
              int place=0;          
              for(int j=0; j<size; j++)
                   Edge temp = (Edge)edge.get(j);
                   if(temp.getSource() == userSource)
                        pathArray[place] = LinkedList.add(temp); //Confused here
                        place++;
              }Assuming Edge is working which is does, how do i add an object to the list that is at position 0 in the array? I know what i did at the line I said I was confused at is wrong but not sure how it should be?
    Could someone help?

    Ok actually ignore that first code, I have changed it as follows which makes more sense but doesnt quite work:
                             ArrayList path = new ArrayList();
              ArrayList pathArray[] = new ArrayList[20];
              int place=0;
              for(int j=0; j<size; j++)
                   Edge temp = (Edge)edge.get(j);
                   if(temp.getSource() == userSource)
                        pathArray[place] = path.add(new Edge(temp));
                        place++;
              }I get an error:
    C:\Users\Taurus\Desktop\University\AMI - 4517\Assignment\branchb>javac BranchB2.java
    BranchB2.java:85: incompatible types
    found : boolean
    required: java.util.ArrayList
    pathArray[place] = path.add(new Edge(temp));
    ^
    1 error
    How can i fix that?

  • Urgent problem : array of linked list

    In order to save memory I am trying to build an array of linkedlist with the following program. It compiles well but when I execute it, error occurs as follows,
    Exception in thread "main" java.lang.NullPointerException
    at testLL2.main(testLL2.java:10)
    My program:
    import java.net.*;
    import java.io.*;
    import java.util.LinkedList;
    public class testLL2 {
    public static void main(String[] args) throws IOException {
    int myLLLength;
    LinkedList myLL2 [] = new LinkedList[10];
    for (int i = 0; i < 10; i++) {
    for (int j=0; j < 5; j++) {
    myLL2.add(" "+j);
    for (int i = 0; i < 10; i++) {
    myLLLength = myLL2[i].size();
    for (int j=0; j<myLLLength; j++) {
    System.out.println(myLL2[i].get(j));
    Since I tried many alternatives to solve it but one of them succeed. Please kindly help me! Thanks in advance!

    [snip]
    2. Where you have 'myLL2.add(" "+j);' it should read
    myLL2. The is probably a typo. If not, make sure
    you understand why you need to change your code.
    The forum posting code will muck up your code if you don't surround it in [ code] [code ] (no spaces) tags.  Even if you do use the code tags, it preprocesses [ i] into <i> to avoid turning your code into italics.

  • Copying an array of linked lists

    I have this:
    ArrayList pathArray[] = new ArrayList[20];
    ArrayList copyRow = pathArray[0];
    pathArray[1] = copyRow;The bottom bit of the above code after the .... doesnt seem to do what i want, I am trying to copy the contents of array index 0 to array index 1. Now when i do that it works but if i start editing the variables in it it changes the variables in index 0, whats the correct way of copying?

    ArrayList pathArray[] = new ArrayList[20];
    Object copyRow  = pathArray[0].clone();
    pathArray[1] = (ArrayList) copyRow;Edited by: Chicon on Apr 25, 2009 8:40 PM

  • Need help on creating a linked list with array...

    I want to create a linked listed using array method. Any suggestion on how would I start it.
    thanks

    That means I want to implement linked list using an
    array.I believe you mean you want to implement list using an array. Linked list is a very different implementation of a list, which can be combined with arrays for a niche implementation of list, but I don't think this is what you're talking about.
    Do you mind if we ask why you don't want to use ArrayList? It does exactly what you specify, plus it will increase the size of the internal array as necessary to ensure it can store the required elements.
    If you really want to make your own, have a look at the source for ArrayList (in src.jar in your JDK installation folder) - it will answer any questions you have. If you don't understand any of the code, feel free to post the relevant code here and ask for clarification.

  • Help on Linked List

    Hey people, i need a small help on my linked list.
    Ok, what i'm trying to do is have a search function with a linked list using strings. First i will have a string, i will then compute the ASCII value for that and take MOD 71 lets say. For example, a string has an ASCII value of 216, MOD 71 of this gives me 3. I will then have an array of 71, and each array will point to a linked list. There will be as many linked list as there are of arrays (71). I will then store this string at index [3] in the linked list in which the array points to.
    My problem for now is, how do i for example create 71 linked list? So far i can create only 1. To create another is easy, but if i have 71 the code will be too much. Is there an iterator i could use that easily creates 71? Sorry about this, i'm not really good with linked list with Java. Anyway, here is what i have now :).
    public class MyLinkedListElement {   
            String string;
            MyLinkedListElement next;
            public MyLinkedListElement(String s) {   
                string = s;
                next = null;   
            public MyLinkedListElement getNext() {
                return next;       
            public String getValue() {
                return string;
    public class MyLinkedList {
        public static MyLinkedListElement head;
        public MyLinkedList() {   
            head = null;
       public static void addToHead(String s) {
            MyLinkedListElement a = new MyLinkedListElement(s);
            if (head == null) {
                head = a;
            } else {
                a.next = head;
                head = a;       
        public static void main(String[] args) {
              String[] arr = {"Bubble", "Angie", "Bob", "Bubble", "Adam"};
              for (int i = 0; i < arr.length; i++) {
                          addToHead(arr);
    MyLinkedListElement current = head;
    while (current != null) {
    System.out.println(current.getValue());
    current = current.next;
    }I can have an array of strings and easily add them to a linked list.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    I am very confused now, i just need to store a string value inside an array of linked list. But i can't somehow do it. I am very close, i can store the value in a linked list but the array of linked list, this is what i have.
    public class MyLinkedListElement {   
            String string;
            MyLinkedListElement next;
            public MyLinkedListElement(String s) {   
                string = s;
                next = null;   
            public MyLinkedListElement getNext() {
                return next;       
            public String getValue() {
                return string;
    public class MyLinkedList {
        public MyLinkedListElement head;
        public MyLinkedList() {   
              head = null;
        public void addToHead(String s) {
             MyLinkedListElement  a = new MyLinkedListElement(s);
             MyLinkedList[] arrayOfLinkedLists = new MyLinkedList[71];
             for(int i = 0; i < arrayOfLinkedLists.length; i++) {
                 arrayOfLinkedLists[i] = new MyLinkedList();
             if (head == null) {    
                 head = a;
             } else {
                 a.next = head;
                 head = a;       
        public void print(MyLinkedListElement current) {
              while (current  != null) {
                    System.out.println(current.getValue());
                    current = current.next;
        public static void main(String[] args) {
              String[] arr = {"Bubble", "Angie", "Bob", "Bubble", "Adam"};
              MyLinkedList listInstance = new MyLinkedList();
              for (int i = 0; i < arr.length; i++) {
                    listInstance.addToHead(arr);
    MyLinkedListElement current = listInstance.head;
    listInstance.print(current);

  • Multiple Instances of Linked List

    i need to create multiple linked lists; the amount of linked lists i'm creating is based on a user inputted value. what's the quickest and most convient way to do this?

    This is another question based on my post yesterday .
    . . . is it possible to create a JTable with an array
    of Linked Lists? i'm trying but i'm getting a complie
    error that says
    " constructor JTable
    (java.util.LinkedList[],java.lang.String[]) not found
    in class javax.swing.JTable "
    i'm calling the method createTable and passing an
    array of LinkedList to it.The error you're getting from the compiler should tell you quite plainly that no, it's not possible. At least not that way.
    Look at the API docs and see if there's a constructor that does take an array of Lists. If not, you'll have to figure out how to make your data fit into what it does take.

  • Please Hilp me ( about linked list)

    i am an university student
    i was write a proram by ( arrays)
    thes program about Employees ( Add , Searsh , displly , delete )
    it is 6 class
    1 = Employee.java
    2 = Boss.java
    3 = CommissionWorker.java
    4 = HourlyWorker.java
    5 = PieceWorker.java
    6 = StartProgram.java
    i wona now changing the program from (Arrays) to (linked list)
    this is the cood

    The last 1
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class StartProgram extends JFrame
              // The Components For Frame 1
              private JLabel Label_Number_Of_Employee ;
              private JTextField Text_Number_Of_Employee ;
              private JButton Button_New, Button_Display, Button_Search, Button_UpData, Button_Delete;
              private JButton Button_Exit, Button_RE;
              private JPanel Panel_1_North, Panel_1_West, Panel_1_South, Panel_1_Center;
              // The Components For Frame 2
              private JLabel Label_Name, Label_Adress, Label_Gread, Label_ID, Label_Salary , Label_Commiss, Label_Quant, Label_Hour, Label_Wage, Label_Pice, Label_Total_Mony, Label_NumberID_FOR_Searsh;
              private JTextField TextField_Total_mony, TextField_Name, TextField_Adress, TextField_Greade,     TextField_ID, TextField_Salary , TextField_Commiss, TextField_Quant, TextField_Hour, TextField_Wage, TextField_Pice, TextField_NumberID_FOR_Searsh;
              private JButton Button_Clear, Button_OK2, Button_Cancel, Button_Next, Button_Prev, Button_Searsh, Button_Save, Button_Delet, Button_Create;
              private JRadioButton rad_Boss, rad_HourlyWorker, rad_PiceWorker, rad_CommissWorker;
              private ButtonGroup radGroup;
              private JPanel Panel_2_Center, Panel_2_South, Panel_2_North;
              // The Container For Frame 1
              Container cont;
              // The Container For Frame 2
              Container cont2;
              // The Layouts For Frame 1 & 2
              private FlowLayout layout;
              private GridLayout glayout;
              // private class Show_Frame2 extends JFrame
              int ID_Conter_Number[] = new int[1];
              Show_Frame2 Frame_Display;
              Show_Frame2 Frame_Delete;
              Show_Frame2 Frame_UpData;
              Show_Frame2 Frame_Search;
              // VARIABLES
              int Text_Number_TO_int = 0;
              int Countr_Index_Of_ArrayEmp = 0;
              int Number_Chose_Acion;
              int Conter_next = -1;
              int Number_Stop_Create = 0;
              int NumberID_FOR_Searsh = 0 ;
              int Nu_S_S_D_F_T_F_T_T_A; // use in Set_Save_Data_From_Text_Field_To_The_Arayy();
              int Number_Set_Kind_Employee; // use in Set_Save_Data_From_Text_Field_To_The_Arayy();
              int Nu_S_K_E_B_R_S; // use in Set_Kind_Employee_By_radBotton_Selected()
              int Nu_S_D_I_F_T_S_B_S_O_D; // use in Set_Data_in_Folds_To_Show_By_Searsh_Or_Disply()
              // use in Set_Save_Data_From_Text_Field_To_The_Arayy()
              int ID_set_Save_Data;
              int Greade_set_Save_Data;
              String Adress_set_Save_Data;
              String Name_set_Save_Data;
              double Salary_set_Save_Data;
              int Quant_set_Save_Data;
              int Hour_set_Save_Data;
              int Pice_set_Save_Data;
              double Wage_set_Save_Data;
              double Commiss_set_Save_Data;
              double Total_Mony;
              // For updata
              int ID_updata;
              int Greade_updata;
              String Adress_updata;
              String Name_updata;
              double Salary_updata;
              int Quant_updata;
              int Hour_updata;
              int Pice_updata;
              double Wage_updata;
              double Commiss_updata;
              double Total_Mony_updata;
              // Arayy(s) To Keep Elements Data Of Employee
              String ArayyName[];
              String ArayyAdress[];
              int ArayyGreade[];
              int ArayyID[];
              double ArayySalary[];
              double ArayyCommiss[];
              int ArayyQuant[];
              int ArayyHour[];
              double ArayyWage[];
              int ArayyPice[];
              int ArayyKind_Emp[];
              // Arayy(s)2 To Keep A Copy Of Elements Data Of Employee
              // Use in Delete Action
              String ArayyName_Copy[];
              String ArayyAdress_Copy[];
              int ArayyGreade_Copy[];
              int ArayyID_Copy[];
              double ArayySalary_Copy[];
              double ArayyCommiss_Copy[];
              int ArayyQuant_Copy[];
              int ArayyHour_Copy[];
              double ArayyWage_Copy[];
              int ArayyPice_Copy[];
              int ArayyKind_Emp_Copy[];
              // CONSRUCTOR of StartProgram class
              // Open form1
              public StartProgram()
                        super("Start To Employee System Control");
                        cont = getContentPane();
                        Panel_1_North = new JPanel();      Panel_1_North.setLayout(new GridLayout(3,0,0,0));
                        Panel_1_Center = new JPanel();          Panel_1_Center.setLayout(new GridLayout(6,0,0,0));
                        Panel_1_South = new JPanel();          Panel_1_South.setLayout(new GridLayout(1,0,0,0));
                        Label_Number_Of_Employee = new JLabel("Enter The Number Of Employees Then Press (ENTER) In Your KeyBoard ! ");
                        Text_Number_Of_Employee = new JTextField(10);
                        // 1- Display Button Choice To Employee System Control in form1
                        // 2- set Buttons Edit = false
                        Button_New = new JButton("Create a new an Employee(s) ");     Button_New.setVisible(false);
                        Button_Display = new JButton("Display an Employee(s) ");     Button_Display.setVisible(false);
                        Button_Search = new JButton("Search for an Employee(s) ");     Button_Search.setVisible(false);
                        Button_UpData = new JButton("UpData for an Employee(s) ");     Button_UpData.setVisible(false);
                        Button_Delete = new JButton("Delete an Employee(s) "); Button_Delete.setVisible(false);
                        // 1- Display Button ( EXIT & START AGEN ) in form1
                        // 2- set Buttons Edit = false
                        Button_Exit = new JButton("EXIT");     Button_Exit.setEnabled(false);
                        Button_RE = new JButton("START AGEN");     Button_RE.setEnabled(false);
                        // an object from the private class ActionHandler
                        ActionHandler handler = new ActionHandler();
                        Button_New.addActionListener(handler);
                        Button_Display.addActionListener(handler);
                        Button_Search.addActionListener(handler);
                        Button_UpData.addActionListener(handler);
                        Button_Delete.addActionListener(handler);
                        Button_Exit.addActionListener(handler);
                        Button_RE.addActionListener(handler);
                        Text_Number_Of_Employee.addActionListener(handler);
                        // Add all objects in form1
                        Panel_1_North.add(Label_Number_Of_Employee);
                        Panel_1_North.add(Text_Number_Of_Employee);
                        Panel_1_Center.add(Button_New);
                        Panel_1_Center.add(Button_Display);
                        Panel_1_Center.add(Button_Search);
                        Panel_1_Center.add(Button_UpData);
                        Panel_1_Center.add(Button_Delete);
                        Panel_1_South.add(Button_Exit);
                        Panel_1_South.add(Button_RE);
                        cont.add(Panel_1_North , BorderLayout.NORTH);
                        cont.add(Panel_1_Center , BorderLayout.CENTER);
                        cont.add(Panel_1_South , BorderLayout.SOUTH);
                        // setting of form1
                        setSize(450,350);
                        setVisible(true);
                        setResizable(false);
              } // END public StartProgram()
              private class ActionHandler implements ActionListener
                        public void actionPerformed(ActionEvent EEEE)
                                  // ================================================
                                  // ================================================
                                  // if user Put A Number Then He clock Enter
                                  if(EEEE.getSource() == Text_Number_Of_Employee)
                                            Set_Enter_Number_Of_Employee_Start();
                                  // ================================================
                                  // ================================================
                                  // Or if user clock Button Exit
                                  else if(EEEE.getSource() == Button_Exit)
                                            System.gc();
                                            System.exit(0);
                                  // ================================================
                                  // ================================================
                                  // if user clock Button RE START
                                  if(EEEE.getSource() == Button_RE)
                                            Set_Program_Start_Agen();
                                  // ================================================
                                  // ================================================
                                  // if user clock Button Create a new Employee
                                  if(EEEE.getSource() == Button_New)
                                            Number_Chose_Acion = 1;
                                            Show_Frame2 CCCC = new Show_Frame2();
                                  // ================================================
                                  // ================================================
                                  // if user clock Button Display The Employee
                                  if(EEEE.getSource() == Button_Display)
                                            Number_Chose_Acion = 2;
                                            Show_Frame2 CCCC = new Show_Frame2();
                                  // ================================================
                                  // ================================================
                                  // if user clock Button Search The Employee
                                  if(EEEE.getSource() == Button_Search)
                                            Number_Chose_Acion = 3;
                                            Show_Frame2 CCCC = new Show_Frame2();
                                  // ================================================
                                  // ================================================
                                  // if user clock Button UpData The Employee
                                  if(EEEE.getSource() == Button_UpData)
                                            Number_Chose_Acion = 4;
                                            Show_Frame2 CCCC = new Show_Frame2();
                                  // ================================================
                                  // ================================================
                                  // if user clock Button Delete
                                  if(EEEE.getSource() == Button_Delete)
                                            Number_Chose_Acion = 5;
                                            Show_Frame2 CCCC = new Show_Frame2();
                        } // END public void actionPerformed(ActionEvent e)
              } // END private class ActionHandler implements ActionListener
              public void Set_Enter_Number_Of_Employee_Start()
                        try
                                  Text_Number_TO_int = Integer.parseInt(Text_Number_Of_Employee.getText());
                                  if(Text_Number_TO_int <= 0)
                                            Text_Number_Of_Employee.setText("ERROR !! Please Enter a Number More Than 0");
                                  else
                                            Set_Create_Lingth_Of_Arayy_By_Number_That_Entered();
                        catch(NumberFormatException nfe)
                                  Text_Number_Of_Employee.setText("ERROR !! Please Enter a Number");
              public void Set_Create_Lingth_Of_Arayy_By_Number_That_Entered()
                        ID_Conter_Number = new int[Text_Number_TO_int];
                        Text_Number_Of_Employee.setEditable(false);
                        Button_New.setVisible(true);
                        Button_Display.setVisible(true);
                        Button_Search.setVisible(true);
                        Button_UpData.setVisible(true);
                        Button_Delete.setVisible(true);
                        Button_Exit.setEnabled(true);
                        Button_RE.setEnabled(true);
                        ArayyName = new String[Text_Number_TO_int];
                        ArayyAdress = new String[Text_Number_TO_int];
                        ArayyGreade = new int[Text_Number_TO_int];
                        ArayyID = new int[Text_Number_TO_int];
                        ArayySalary = new double[Text_Number_TO_int];
                        ArayyCommiss = new double[Text_Number_TO_int];
                        ArayyQuant = new int[Text_Number_TO_int];
                        ArayyHour = new int[Text_Number_TO_int];
                        ArayyWage = new double[Text_Number_TO_int];
                        ArayyPice = new int[Text_Number_TO_int];
                        ArayyKind_Emp = new int[Text_Number_TO_int];
              public void Set_Program_Start_Agen()
                        Text_Number_Of_Employee.setEditable(true);
                        Text_Number_Of_Employee.setText("");
                        Countr_Index_Of_ArrayEmp = 0;
                        Conter_next = -1;
                        Text_Number_TO_int = 0;
                        Countr_Index_Of_ArrayEmp = 0;
                        Conter_next = -1;
                        Nu_S_S_D_F_T_F_T_T_A = 0;
                        Number_Set_Kind_Employee = 0;
                        Number_Stop_Create = 0;
                        ArayyName = null ;
                        ArayyAdress = null ;
                        ArayyGreade = null ;
                        ArayyID = null ;
                        ArayySalary = null ;
                        ArayyCommiss = null ;
                        ArayyQuant = null ;
                        ArayyHour = null ;
                        ArayyWage = null ;
                        ArayyPice = null ;
                        ArayyKind_Emp = null ;
                        ArayyName_Copy = null ;
                        ArayyAdress_Copy = null ;
                        ArayyGreade_Copy = null ;
                        ArayyID_Copy = null ;
                        ArayySalary_Copy = null ;
                        ArayyCommiss_Copy = null ;
                        ArayyQuant_Copy = null ;
                        ArayyHour_Copy = null ;
                        ArayyWage_Copy = null ;
                        ArayyPice_Copy = null ;
                        ArayyKind_Emp_Copy = null ;
                        ID_set_Save_Data = 0;
                        Greade_set_Save_Data = 0;
                        Adress_set_Save_Data = null;
                        Name_set_Save_Data = null;
                        Salary_set_Save_Data = 0.00;
                        Quant_set_Save_Data = 0;
                        Hour_set_Save_Data = 0;
                        Pice_set_Save_Data = 0;
                        Wage_set_Save_Data = 0.00;
                        Commiss_set_Save_Data = 0.00;
              // Whin User Click Any rad Button
              private class RadHandler implements ItemListener
                        public void itemStateChanged(ItemEvent i)
                                  Set_All_Text_Field_To_Defolt_Text();
                                  TextField_Name.setEditable(true);
                                  TextField_Adress.setEditable(true);
                                  TextField_Greade.setEditable(true);
                                  if(i.getSource() == rad_Boss)
                                            TextField_Salary.setEditable(true);
                                            TextField_Wage.setEditable(false);
                                            TextField_Commiss.setEditable(false);
                                            TextField_Quant.setEditable(false);
                                            TextField_Hour.setEditable(false);
                                            TextField_Pice.setEditable(false);
                                  else if(i.getSource() == rad_HourlyWorker)
                                            TextField_Salary.setEditable(false);
                                            TextField_Wage.setEditable(true);
                                            TextField_Hour.setEditable(true);
                                            TextField_Commiss.setEditable(false);
                                            TextField_Quant.setEditable(false);
                                            TextField_Pice.setEditable(false);
                                  else if(i.getSource() == rad_PiceWorker)
                                            TextField_Salary.setEditable(false);
                                            TextField_Wage.setEditable(true);
                                            TextField_Hour.setEditable(false);
                                            TextField_Quant.setEditable(false);
                                            TextField_Commiss.setEditable(false);
                                            TextField_Pice.setEditable(true);
                                  else if(i.getSource() == rad_CommissWorker)
                                            TextField_Salary.setEditable(true);
                                            TextField_Wage.setEditable(false);
                                            TextField_Hour.setEditable(false);
                                            TextField_Quant.setEditable(false);
                                            TextField_Commiss.setEditable(true);
                                            TextField_Pice.setEditable(true);
              private class Show_Frame2 extends JFrame
                        public Show_Frame2()
                                  super("Control Panal Of Employee's Data");
                                  cont2 = getContentPane();
                                  Panel_2_Center = new JPanel();
                                  Panel_2_Center.setLayout(new GridLayout(11,2,5,5));
                                  Panel_2_South = new JPanel();
                                  Panel_2_South.setLayout(new GridLayout(2,5,5,5));
                                  Panel_2_North = new JPanel();
                                  Panel_2_North.setLayout(new GridLayout(2,1,5,5));
                                  Label_NumberID_FOR_Searsh = new JLabel("Employee's ID :", SwingConstants.RIGHT);
                                  Label_NumberID_FOR_Searsh.setVisible(false);
                                  TextField_NumberID_FOR_Searsh = new JTextField(5);
                                  TextField_NumberID_FOR_Searsh.setVisible(false);
                                  TextField_NumberID_FOR_Searsh.setEditable(false);
                                  Label_Name = new JLabel("Name : " , SwingConstants.RIGHT);
                                  Label_Adress = new JLabel("Address : " , SwingConstants.RIGHT);
                                  Label_Gread = new JLabel("Greade : " , SwingConstants.RIGHT);
                                  Label_ID = new JLabel("ID : " , SwingConstants.RIGHT);
                                  Label_Salary = new JLabel("Salary : " , SwingConstants.RIGHT);
                                  Label_Commiss = new JLabel("Commiss : " , SwingConstants.RIGHT);
                                  Label_Quant = new JLabel("Quant : " , SwingConstants.RIGHT);
                                  Label_Hour = new JLabel("Hours : " , SwingConstants.RIGHT);
                                  Label_Wage = new JLabel("Wage : " , SwingConstants.RIGHT);
                                  Label_Pice = new JLabel("Pice : " , SwingConstants.RIGHT);
                                  Label_Total_Mony = new JLabel("Total_Money : " , SwingConstants.RIGHT);
                                  TextField_Name = new JTextField(45);
                                  TextField_Adress = new JTextField(45);
                                  TextField_Greade = new JTextField(2);
                                  TextField_ID = new JTextField(4);
                                  TextField_Salary = new JTextField(10);
                                  TextField_Commiss = new JTextField(10);
                                  TextField_Quant = new JTextField(10);
                                  TextField_Hour = new JTextField(10);
                                  TextField_Wage = new JTextField(10);
                                  TextField_Pice = new JTextField(10);
                                  TextField_Total_mony = new JTextField(10); TextField_Total_mony.setEditable(false);
                                  rad_Boss = new JRadioButton("Boss" , false);
                                  rad_HourlyWorker = new JRadioButton("Hourly" , false);
                                  rad_PiceWorker = new JRadioButton("Pice" , false);
                                  rad_CommissWorker = new JRadioButton("Commiss" , false);
                                  radGroup = new ButtonGroup();
                                  radGroup.add(rad_Boss);
                                  radGroup.add(rad_HourlyWorker);
                                  radGroup.add(rad_PiceWorker);
                                  radGroup.add(rad_CommissWorker);
                                  //handler is object of private class RadHandler
                                  RadHandler rHandler = new RadHandler();
                                  rad_Boss.addItemListener(rHandler);
                                  rad_HourlyWorker.addItemListener(rHandler);
                                  rad_PiceWorker.addItemListener(rHandler);
                                  rad_CommissWorker.addItemListener(rHandler);
                                  Button_Save = new JButton("Save"); Button_Save.setVisible(false);
                                  Button_Delet = new JButton("Delete"); Button_Delet.setVisible(false);
                                  Button_Searsh = new JButton("Search"); Button_Searsh.setVisible(false);
                                  Button_Next = new JButton("next");
                                  Button_Prev = new JButton("Prev");
                                  Button_Clear = new JButton("Clear");
                                  Button_OK2 = new JButton("OK"); Button_OK2.setEnabled(false);
                                  Button_Cancel = new JButton("Cancel/Menu");
                                  Button_Create = new JButton("Create");
                                  // ================================================
                                  // ================================================
                                  // of usrt click Button Clear
                                  Button_Clear.addActionListener
                                            new ActionListener()
                                                      public void actionPerformed(ActionEvent e)
                                                                Set_Part_Of_Text_Field_Clear_of_Text();
                                  // ================================================
                                  // ================================================
                                  // of usrt click Button Cancel / Menu
                                  Button_Cancel.addActionListener
                                            new ActionListener()
                                                      public void actionPerformed(ActionEvent e)
                                                                setVisible(false);
                                  // ================================================
                                  // ================================================
                                  // 111111111111111111111111111111111111111111111111//
                                  // (Create new Employee)
                                  if (Number_Chose_Acion == 1)
                                            if (Number_Stop_Create >= Text_Number_TO_int)
                                                      JOptionPane.showMessageDialog(cont2, "You Can not add more Employees " , "Createing Will stop",JOptionPane.INFORMATION_MESSAGE);
                                            else
                                                      set_Show_Create_employees_Form();
                                  // ================================================
                                  // ================================================
                                  // 222222222222222222222222222222222222222222222222//
                                  // ( Display Employee)
                                  else if (Number_Chose_Acion == 2)
                                            if (ArayyName[0] == null)
                                                      JOptionPane.showMessageDialog(cont2,"Ther are No Employees to Display", "Exception Display",JOptionPane.ERROR_MESSAGE);
                                            else
                                                      set_Show_Display_employees_Form();
                                  // ================================================
                                  // ================================================
                                  // 333333333333333333333333333333333333333333333333//
                                  // ( Search Employee)
                                  else if (Number_Chose_Acion == 3)
                                            try
                                                      if (ArayyName[0] == null)
                                                                JOptionPane.showMessageDialog(cont2,"Ther are No Employees to Search", "Exception Search",JOptionPane.ERROR_MESSAGE);
                                                      else
                                                                set_Show_Search_employees_Form();
                                            catch (Exception ex)
                                                      JOptionPane.showMessageDialog(cont2,ex, "Exception",JOptionPane.ERROR_MESSAGE);
                                  // ================================================
                                  // ================================================
                                  // 444444444444444444444444444444444444444444444444//
                                  // ( UpData Employee)
                                  else if (Number_Chose_Acion == 4)
                                            try
                                                      if (ArayyName[0] == null)
                                                                JOptionPane.showMessageDialog(cont2,"Ther are No Employees to UpData", "Exception UpData",JOptionPane.ERROR_MESSAGE);
                                                      else
                                                                set_Show_UpData_employees_Form();
                                            catch (Exception ex)
                                                      JOptionPane.showMessageDialog(cont2,ex, "Exception",JOptionPane.ERROR_MESSAGE);
                                  // ================================================
                                  // ================================================
                                  // 555555555555555555555555555555555555555555555555//
                                  // ( Delete Action )
                                  else if (Number_Chose_Acion == 5)
                                            try
                                                      if (ArayyName[0] == null)
                                                                JOptionPane.showMessageDialog(cont2,"Ther are No Employees to Delete", "Exception Delete",JOptionPane.ERROR_MESSAGE);
                                                      else
                                                                set_Show_Delete_employees_Form();
                                            catch (Exception ex)
                                                      JOptionPane.showMessageDialog(cont2,ex, "Exception",JOptionPane.ERROR_MESSAGE);
                                  // ================================================
                                  // ================================================
                        } // END public Show_Frame2()
                        // 11111111111111111111111111111111111111111
                        public void set_Show_Create_employees_Form()
                                  set_Container_Of_Form2();
                                  setTitle("Create employees");
                                  setResizable(false);
                                  setSize(600,500);
                                  setVisible(true);
                                  Set_TextField_Editable_IS_FALSE();
                                  Conter_next = 0;
                                  // whin Fram Create new Employee loading
                                  Button_Next.setEnabled(false);
                                  Button_Prev.setEnabled(false);
                                  // of usrt click Button Create
                                  Button_Create.addActionListener
                                            new ActionListener()
                                                      public void actionPerformed(ActionEvent e)
                                                                // Kind of Employee that will ad is boss
                                                                if(rad_Boss.isSelected() == true)
                                                                          try
                                                                                    int Greade_N = Integer.parseInt(TextField_Greade.getText());
                                                                                    if ( (Greade_N > 99) || (Greade_N < 1) )
                                                                                                   JOptionPane.showMessageDialog(cont2," Please\nEnter Number in (Greade) Between 1 To 99", "ERROR NUMBER",JOptionPane.ERROR_MESSAGE);
                                                                                    else
                                                                                              Nu_S_S_D_F_T_F_T_T_A = Countr_Index_Of_ArrayEmp;
                                                                                              Number_Set_Kind_Employee = 1; // = Boss
                                                                                              Set_Save_Data_From_Text_Field_To_The_Arayy();
                                                                                              Set_All_Text_Field_To_Defolt_Text();
                                                                                              JOptionPane.showMessageDialog(cont2,Name_set_Save_Data + "\nwas Created As Boss", "Create",JOptionPane.INFORMATION_MESSAGE);
                                                                                              Countr_Index_Of_ArrayEmp ++;
                                                                                              Number_Stop_Create ++;
                                                                          catch(Exception ex)
                                                                                    JOptionPane.showMessageDialog(cont2,"Please Enter Correct Data\n"+ex, "Exception",JOptionPane.ERROR_MESSAGE);
                                                                // Kind of Employee that will ad is rad_HourlyWorker
                                                                if(rad_HourlyWorker.isSelected() == true)
                                                                               try
                                                                                    int Greade_N = Integer.parseInt(TextField_Greade.getText());
                                                                                    if ( (Greade_N > 99) || (Greade_N < 1) )
                                                                                              JOptionPane.showMessageDialog(cont2," Please\nEnter Number in (Greade) Between 1 To 99", "ERROR NUMBER",JOptionPane.ERROR_MESSAGE);
                                                                                    else
                                                                                              Nu_S_S_D_F_T_F_T_T_A = Countr_Index_Of_ArrayEmp;
                                                                                              Number_Set_Kind_Employee = 2; // = HourlyWorker
                                                                                              Set_Save_Data_From_Text_Field_To_The_Arayy();
                                                                                              Set_All_Text_Field_To_Defolt_Text();
                                                                                              JOptionPane.showMessageDialog(cont2,Name_set_Save_Data + "\nwas Created As HourlyWorker", "Create",JOptionPane.INFORMATION_MESSAGE);
                                                                                              Countr_Index_Of_ArrayEmp ++;
                                                                                              Number_Stop_Create ++;
                                                                          catch(Exception ex)
                                                                                    JOptionPane.showMessageDialog(cont2,ex, "Exception",JOptionPane.ERROR_MESSAGE);
                                                                // Kind of Employee that will ad is rad_PiceWorker
                                                                if(rad_PiceWorker.isSelected() == true)
                                                                          try
                                                                                    int Greade_N = Integer.parseInt(TextField_Greade.getText());
                                                                                    if ( (Greade_N > 99) || (Greade_N < 1) )
                                                                                              JOptionPane.showMessageDialog(cont2," Please\nEnter Number in (Greade) Between 1 To 99", "ERROR NUMBER",JOptionPane.ERROR_MESSAGE);
                                                                                    else
                                                                                              Nu_S_S_D_F_T_F_T_T_A = Countr_Index_Of_ArrayEmp;
                                                                                              Number_Set_Kind_Employee = 3; // = PiceWorker
                                                                                              Set_Save_Data_From_Text_Field_To_The_Arayy();
                                                                                              Set_All_Text_Field_To_Defolt_Text();
                                                                                              JOptionPane.showMessageDialog(cont2,Name_set_Save_Data + "\nwas Created As PiceWorker", "Create",JOptionPane.INFORMATION_MESSAGE);
                                                                                              Countr_Index_Of_ArrayEmp ++;
                                                                                              Number_Stop_Create ++;
        

Maybe you are looking for