Array of structures


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Array of structures

  1. #1
    Join Date
    Oct 2004
    Posts
    9

    Smile Array of structures

    Hello,
    I've looked around and cannot seem to find how to create and array of this structure. I can instantiate the class PCB just fine but I can't seem to figure out how to create an array of them. This is a small amount of my code. Can anyone help me?

    Code:
    import java.io*;
    import java.lang.*;
    import java.util.*;
    
    public class ossim{
    
       public static void main(String[] args) throws IOException{
    
          PCB newProcess = new PCB();  /* how do i create an array of these?       */
                                                    /* ie.  "PCB newProcess[10] = new PCB(); */
    
       }
    
    }
    
    class PCB{
       int PID;              /* process id number     */
       String name;       /* name given from input */
       int priority;         /* priority of process   */
       String statusType;
       LinkedList statusList = new LinkedList();
       int[] crTree = new int[10];
       LinkedList otherResources = new LinkedList();
    }

  2. #2
    Join Date
    Jan 2005
    Posts
    45
    Hello,

    An array of objects cannot be allocated in one statement. You can allocate an array of object references, but the objects themselves must independently be created.So, you may try:

    PCB[] newProcess = new PCB[10]; // create array of references
    newProcess[0] = new PCB(); // create element
    newProcess[1] = new PCB(); // create element
    ...

    Hope that helps,
    Lionel Badiou
    CodeFutures -
    Java Code Generation

    http://www.codefutures.com



  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Well, ... this is one statement...
    Code:
    PCB[]newProcess = new PCB[]{
      new PCB(), 
      new PCB()
    };
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center