Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: unique identifier

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default unique identifier

     
    [B]public class b[/B] {
     
    	static a[] nodeobj=new a[3];
     
    	public static void main(String[] args) {
    		nodeobj[0]=new a(123);
    		nodeobj[1]=new a(44);
    		nodeobj[2]=new a(3);}
    [B]public class a[/B] {
     
    	int x;
    	  c[] datalist=new c[3];
     
    	public a(int a)
    	{	x=a;
    	}}
     
     
     
    [B]public class c [/B]{
     
    	int t; 	
    	public c(int y)
    	{
     
    		t=y;
    	}

    how to createunique refernces so that each nodeobj has its own datalist..

    nodeobj[0] ---> datalist[o]
    ---> datalist[1]
    ---> datalist[2]

    nodeobj[1] ---> datalist[o]
    ---> datalist[1]
    ---> datalist[2]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unique identifier

    Please explain what you want to do. Your posted "examples" don't have any meaning for me.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: unique identifier

    Norm,

    i want :

     nodeobj[0].datalist[0].t 
    nodeobj[0].datalist[1].t 
    nodeobj[0].datalist[2].t 
     
     
    nodeobj[1].datalist[0].t 
    nodeobj[1].datalist[1].t 
    nodeobj[1].datalist[2].t

    to be unique for each nodeobj
    so that we can have


    nodeobj[0].datalist[0].t
    nodeobj[0].datalist[1].t
    nodeobj[0].datalist[2].t


    nodeobj[1].datalist[0].t
    nodeobj[1].datalist[1].t
    nodeobj[1].datalist[2].t


    but puting c[] datalist=new c[3] makes the datalist[] common for each nodeobj

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unique identifier

    Sorry, still do not understand your problem. Your too short pieces of code leave stuff out.
    I see an array: nodeobj of type ??? that has a member: datalist that is an array of type c.
    c has a member t.

    By each nodeobj I assume you're referring to the elements in the nodeobj array.
    Why does the value of the datalist member in nodeobj[0] have to be the same as the value of the datalist member of nodeobj[1]? They are separate instances of the ??? type. Each instance will have its own datalist member with its own contents.

    You'll have to show a set of class definitions in a executable problem that demonstrates the problem.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: unique identifier

    Norm thanks, let me xplain a bit more

    I have a main class in which i make 'nodeobj' objects of node class.
    Each nodeobj has its own location,type etc.


    now i also want a datalist for each nodeobj that can further point to a class data.
    so that each node has a list which has a type -
    so in referencing i can point like
    nodeobj[0].datalist[0].type
    nodeobj[0].datalist[1].type


    for second node like nodeobj[1].datalist[0].type

    can i do that ?? pls . see the attached file Untitled.jpg

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unique identifier

    i also want a datalist for each nodeobj that can further point to a class data.
    If each element in the nodeobj array is a different object, then its contents can be unique to that object.

    Sorry, I don't see your problem yet.
    Write a small simple program that executes to demonstrate what your problem is.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unique identifier

    how can i instantiate the datalist object seperately for each node obj
    Isn't that done in the a class? Each a object gets its own datalist variable.

    Could you comment the code with what you are trying to do? Having to scroll up to read your comments and then scroll down to read the code is rather tedious.

  8. #8
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: unique identifier

    public class b {
     
    		static a[] nodeobj=new a[3];
     
    	public static void main(String[] args) {
    		nodeobj[0]=new a(123);
    		nodeobj[1]=new a(44);
    		nodeobj[2]=new a(3);
     
    tun();	}
     
    public static void tun()
    {
    	System.out.println("node 0 has follwoing elemnts of list"); 	
    	System.out.println(nodeobj[0].datalist[0].type); 
    	System.out.println(nodeobj[0].datalist[1].type);
    	System.out.println(nodeobj[0].datalist[2].type);
    	System.out.println("node 1 has follwoing elemnts of list"); 	
    	System.out.println(nodeobj[1].datalist[0].type); 
    	System.out.println(nodeobj[1].datalist[1].type);
    	System.out.println(nodeobj[1].datalist[2].type);
    	System.out.println("changinf avalues");
    	nodeobj[0].datalist[0].type=6;
    	nodeobj[0].datalist[1].type=66;
    	nodeobj[0].datalist[2].type=666;
    	System.out.println(nodeobj[0].datalist[0].type); 
    	System.out.println(nodeobj[0].datalist[1].type);
    	System.out.println(nodeobj[0].datalist[2].type);
    	System.out.println("changinf avalues");
    	nodeobj[1].datalist[0].type=1;
    	nodeobj[1].datalist[1].type=11;
    	nodeobj[1].datalist[2].type=111;
    	System.out.println(nodeobj[1].datalist[0].type); 
    	System.out.println(nodeobj[1].datalist[1].type);
    	System.out.println(nodeobj[1].datalist[2].type);
     
     
    }
     
    }
     
     
     
     
     
    public class a {
     
    	int x;
    	  c[] datalist=new c[3];
    	  c pkt=new c(43);
     
    	  c po=new c(345);
    	public a(int a)
    	{
    		datalist[0]=new c(88);// i am explicitly instantiating object here can i do that dynamically for particular nodeobj
    		datalist[1]=new c(77);
    		 datalist[2]=new c(99);
     
    	x=a;
    	}
     
    }
     
     
    public class c {
     
    	int type;
     
    	public c(int y)
    	{
    				type=y;
    	}
    public c()
    {
     
    }
    }

    Rite now i have to instantiate class C object "datalist" first which is common for "nodeobj" in class b and then i change the attributes in class b explicitly ... How can i do that dynamically for each nodeobj so that i have nodeobj[0].datalist[0] different from nodeobj[1].datalist[0]

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unique identifier

    If you do this in a's constructor: datalist[0]=new c(88);
    then all instances of an a object will have a c object created with 88. Don't hard code 88 in the constructor. Use a variable if you want them to hold different values. Pass a value in a's constructor to be used to create the c objects.

    What are the c objects supposed to hold?

  10. #10
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: unique identifier

    I want class c's object " datalist" to hold objects by referencing like nodeobj[0].datalist[0]= this object.... but for that i am instantiating the objects in a's constructor...i am doing this so that "datalist" is initialized and there is no null pointer exception when i store objects . how can i make the instatntiation dynamic so that i can use class b to store nodeobj[0].datalist[0]= this object
    nodeobj[0].datalist[1]= this object



    the main taskh here is that i need to check for each nodeobj the datalist and check if its empty(initializing wont let me do taht with !=null) and if empty store pkt object from a class. next time i need to compare the pkt object with datalist if it is available or not
    Last edited by jack_nutt; June 19th, 2011 at 05:24 PM.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unique identifier

    how can i make the instatntiation dynamic so that i can use class b to store ...
    Sorry, I don't see any definition for class b.

    nodeobj[0].datalist[0] is type c
    How is class b related to this?
    Last edited by Norm; June 19th, 2011 at 05:26 PM.

  12. #12
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: unique identifier

    i need nodeobj[0].datalist[0] in class b's method

    the main task here is that i need to check for each nodeobj its datalist and check if its empty(initializing wont let me do taht with !=null) and if empty store pkt object from a class. next time i need to compare the pkt object with datalist if it is available or not

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unique identifier

    Do you know about getters and setters? If a method in class b needs to access data in another class, it callsa method in that class.

    i need to check for each nodeobj its datalist and check if its empty(initializing wont let me do taht with !=null)
    Can you post the code showing where it won't let you do that.

    if (nodeobj[0] != null && nodeobj[0].datalist[0] != null) ...


    i need to compare the pkt object with datalist if it is available or not
    Oh good, another object that is not shown in previous postings.

  14. #14
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: unique identifier

    i did mention pkt object in my coding in previous post ... i found out that i could not explain you my problem perfectly,sorry about that.


    now i want to check the datalist for each nodeobj and if it is empty store "pkt" object in it and if it is not then match the " pkt" if it already exist in the datalist then do ceratain operation .... can u simply my routine

    if ( nodeobj[0].datalist[0] == null) 
    	{
    		nodeobj[0].datalist[0]=nodeobj[0].pkt;
    		System.out.println("stored"); 
     
    	}
    	else
    	{
    		for(int i=0;i<3;i++)
    		{		 if(nodeobj[0] != null && nodeobj[0].datalist[i] != null){
    			if(nodeobj[0].pkt.type==nodeobj[0].datalist[i].type){
    		<do certain operation>
     
     
     
    			}}}
    	}

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unique identifier

    if ( nodeobj[0].datalist[0] == null)
    can nodeobj[0] be null here?
    Last edited by Norm; June 19th, 2011 at 07:22 PM.

  16. #16
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: unique identifier

    No. its like nodeobj[0]'s datalist[0]th element is null -if it has nothing stored....isnt it so ?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unique identifier

    There are two arrays. Can either of them have null entries? Or do you always put something into one or both of them? I can't see your code so I don't know what you have. The code in post#8 filled both of them so neither would be null. If you don't put a value in an array slot, it will be null.

  18. #18
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: unique identifier

    nodeobj[0]!=null ; check is wrong sorry about that

  19. #19
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: unique identifier

    the problem is that the mehtod should run once.


     
    else
    	{
    		for(int i=0;i<3;i++)
    		{		 ifnodeobj[0].datalist[i] != null){
    			if(nodeobj[0].pkt.type==nodeobj[0].datalist[i].type){
    		<do certain operation> ------------------------------------------------------------>the mehtod should  run once.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unique identifier

    the problem is that the mehtod should run once.
    Does that mean the method is never run or that the method is run more than once?

    Your posted code does not show any method.

  21. #21
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: unique identifier

    Norm one last word: Can you please design a code that

    checks the nodeobj[0].datalist[i] list and if it is empty stores nodeobj[0].pkt in it and if not empty compares if node[0].pkt.type is there and if it is not found executes a method.

    i'll be grateful
    thanks

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unique identifier

    I'll help you get your code working. Post what you have and we'll work on it.

  23. #23
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: unique identifier

     	if ( nodeobj[0].datalist[0] == null) // if first element in datalist is empty
    	{
    		nodeobj[0].datalist[0]=nodeobj[0].pkt;
    	 System.out.println("object stored"); 
     
    	}
    	else
    	{
    		for(int i=0;i<3;i++)
    		{		 if((nodeobj[0].datalist[i] != null) &&(nodeobj[0].pkt.type==nodeobj[0].datalist[i].type))
     
    				{
    			 	 < i want that if the match is there ,then come out of this method ,and if no match found store the pkt object and run a method called
    NEXT()
     
    						 }
     
     
     
    	}
     
     
     
    	}

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: unique identifier

    Two ways you could do that
    Have an empty statement for the true case of the if and do the stuff you want done in the else clause.

    if (the match) {
    // do nothing
    }else {
      store pkt
      call NEXT()
    }

    Change the logic to NOT logic. If true it does the stuff you want. This is not exactly the opposite of the previous because it still test if datalist is null, but you have to test it to test its value.
    if( (nodeobj[0].datalist[i] != null) 
        && (nodeobj[0].pkt.type != nodeobj[0].datalist[i].type) ) {
      store pkt
      call NEXT()
    }

  25. The Following User Says Thank You to Norm For This Useful Post:

    jack_nutt (June 19th, 2011)

  26. #25
    Member
    Join Date
    Jun 2011
    Posts
    68
    My Mood
    Cool
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: unique identifier

    Thanks NORm,

    how can i generalize this code in a way that the code checks for the packet.type in the datalist,if its not there it stores it to any available location???



    the problem with the code is that in case of else( the method is run twice ,if the datalist is null)

    thus please suggest me a general code that can check and store in any location
    Last edited by jack_nutt; June 19th, 2011 at 10:48 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. ok i have tried everything but identifier expected keeps popping up!
    By knoxy5467 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: June 5th, 2011, 11:13 PM
  2. Problem with automatically creating new objects with unique names
    By oniamien in forum Java Theory & Questions
    Replies: 2
    Last Post: December 4th, 2010, 02:38 PM
  3. Identifier expected
    By Sphinx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2010, 02:50 PM
  4. <identifier> expected
    By Trunk Monkeey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 09:33 PM
  5. [SOLVED] Java error "Another <identifier> expected"
    By bruint in forum What's Wrong With My Code?
    Replies: 23
    Last Post: May 1st, 2009, 08:47 AM