The Artima Developer Community
Sponsored Link

Java Answers Forum
LinkedList

26 replies on 2 pages. Most recent reply: Oct 8, 2003 12:41 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 26 replies on 2 pages [ 1 2 | » ]
Subha

Posts: 16
Nickname: nikki
Registered: Sep, 2003

LinkedList Posted: Sep 29, 2003 8:13 AM
Reply to this message Reply
Advertisement
Hi,

Let me explain in short what I am trying to do.I am creating a 10x10 array and I am creating a linked list for each cell of the array.I also have 5 point which are made to move in random in the environment.I have a counter which keep tracks of the time that any point takes to visit that particular cell.I need to fit in the time interval in the linked list.
When i tried doing that it gave me an error:
cannot resolve method
method intValue()
java.lang.Object.
What should I do.I can send my program to you if you can give me your email.
Thanks a ton!!


Subha

Posts: 16
Nickname: nikki
Registered: Sep, 2003

Re: LinkedList Posted: Sep 29, 2003 12:59 PM
Reply to this message Reply
Can someone help me with this.
thanks

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: LinkedList Posted: Sep 29, 2003 1:09 PM
Reply to this message Reply
It looks like syntax error. Post the code and someone will help you.

Subha

Posts: 16
Nickname: nikki
Registered: Sep, 2003

Re: LinkedList Posted: Sep 29, 2003 2:23 PM
Reply to this message Reply
I seemed to have corrected the syntax.
This program compiles but does not give the output.it gives outofboundsexception error.
can anyone please help me with that asap.
Thanks.

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;


class uavmaze1
{
public static void main(String args[])
{

System.out.println("UAVs Starts to move");

int i=1, delayCount;

int initCount = 0;

while (i!=6)
{
if(i==1) {
ThreadHandler thread = new ThreadHandler(i,initCount);
thread.start();

}
else
{
initCount++;

ThreadHandler thread = new ThreadHandler(i,initCount);
thread.start();

}

for(delayCount=0;delayCount<=170;delayCount++)
{
delayCount = delayCount++;
}


//increment i to label the next connection
i++;
}

}


}



//define the thread class for handling new connection
class ThreadHandler extends Thread
{
private int number; //label for the connection

public LinkedList[][] uavArray;


public int row, col;

private int index,jndex;

OutputStreamWriter out;

private int inCount;

public int globalTime;

public int a;

public int siz;


public ThreadHandler(int id, int indexCount)

{
number = id;

inCount = indexCount;

row = 1;

col = 1;

}


public void newInit()
{


//Declare and initialize the array

uavArray = new LinkedList[11][11];

System.out.println("i am here");

for(index=1;index<=10;index++)
for(jndex=1;jndex<=10;jndex++)

{
uavArray[index][jndex] = new LinkedList();
uavArray[index][jndex].addFirst(new Integer(0));
}

System.out.println("iam initialized");

index =0;

//All points start from coordinate (1,1)

row =1;

col =1;

globalTime =1;


//open a file and its handle for updation

try {
FileOutputStream fout = new FileOutputStream("uavFile.txt");
OutputStreamWriter out = new OutputStreamWriter(fout, "8859_1");
}

catch (IOException e) {
System.err.println(e);
}
}



public void UAVCoordinates(int number, int posRandom)
{

//Generate Random number between 1 And 8

globalTime++;

System.out.println("I am here2");
switch (posRandom) {

case 1:

//Decrease both row and col -- NW

if(row==1)
row=10;
else
row = row - 1;

if(col==1)
col=10;
else
col = col - 1;



System.out.println("UAV : " + number + " NW - Row :" + row + " Col: " + col + "\n");

siz = (uavArray[row][col].size());

a = ((Integer)(uavArray[row][col].get(siz))).intValue();

uavArray[row][col].addLast(new Integer(globalTime - a));

break;

case 2:


//Decrease row -- North
if(row==1)
row=10;
else
{
row = row - 1;
col = col;
}



System.out.println("UAV : " + number + " N - Row :" + row + " Col: " + col + "\n");



siz = (uavArray[row][col].size());

a = ((Integer)(uavArray[row][col].get(siz))).intValue();

uavArray[row][col].addLast(new Integer(globalTime - a));


break;

case 3:

//Decrease row and Increase col - NE
if(row==1)
row=10;
else
row = row - 1;

if(col==10)
col = 1;
else
col = col + 1;



System.out.println("UAV : " + number + " NE - Row :" + row + " Col: " + col + "\n");



siz = (uavArray[row][col].size());

a = ((Integer)(uavArray[row][col].get(siz))).intValue();

uavArray[row][col].addLast(new Integer(globalTime - a));


break;

case 4:

//Increase col - East

if(col==10)
col = 1;
else
col = col + 1;



System.out.println("UAV : " + number + " E - Row :" + row + " Col: " + col + "\n");



siz = (uavArray[row][col].size());

a = ((Integer)(uavArray[row][col].get(siz))).intValue();

uavArray[row][col].addLast(new Integer(globalTime - a));


break;

case 5:

//Increase Row And Col -- SE

if(col==10)
col = 1;
else
col = col + 1;

if(row==10)
row = 1;
else
row = row + 1;



System.out.println("UAV : " + number + " SE - Row :" + row + " Col: " + col + "\n");



siz = (uavArray[row][col].size());

a = ((Integer)(uavArray[row][col].get(siz))).intValue();

uavArray[row][col].addLast(new Integer(globalTime - a));


break;

case 6:

// Increase row -- South

if(row==10)
row = 1;
else
row = row + 1;



System.out.println("UAV : " + number + " S - Row :" + row + " Col: " + col + "\n");



siz = (uavArray[row][col].size());

a = ((Integer)(uavArray[row][col].get(siz))).intValue();

uavArray[row][col].addLast(new Integer(globalTime - a));


break;

case 7:

//Increase row and Decrease Col -- SW

if(row==10)
row=1;
else
row = row + 1;

if(col==1)
col=10;
else
col = col - 1;



System.out.println("UAV : " + number + " SW - Row :" + row + " Col: " + col + "\n");



siz = (uavArray[row][col].size());

a = ((Integer)(uavArray[row][col].get(siz))).intValue();

uavArray[row][col].addLast(new Integer(globalTime - a));


break;

case 8:

//Decrease col -- W

if(col==1)
col=10;
else
col = col + 1;



System.out.println("UAV : " + number + " W - Row :" + row + " Col: " + col + "\n");



siz = (uavArray[row][col].size());

a = ((Integer)(uavArray[row][col].get(siz))).intValue();

uavArray[row][col].addLast(new Integer(globalTime - a));


break;

default:

}

}



public void run()
{

int inCount=0,i,j,k;

while(inCount!=1000)
{
if(inCount==0)
{
inCount=1;
newInit();
}


Random generator = new Random();

int posRandom = generator.nextInt(7) + 1;

UAVCoordinates(number,posRandom);

inCount++;

}


System.out.println("\n\n");


System.out.println("*******************************************************");
for(i=1;i<=10;i++)
for(j=1;j<=10;j++)
System.out.println("No of visits to location ( " + i + "," + j + ") >> " + uavArray[j] + "\n");

/*System.out.println("*******************************************************") ;
for(i=1;i<=10;i++)
for(j=1;j<=10;j++)
{
System.out.println("Visit Time interval for each cell is >> \n");
for ( k = 0; k < uavArray[j].size(); k++ )
System.out.println( "Row = " + i + " And Col = " + j + "" + uavArray[j].get( k ) + "\2t" );
}*/



} //run closes

} //class threadhandler closed

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: LinkedList Posted: Sep 29, 2003 7:13 PM
Reply to this message Reply
Specifically it throws
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

I think at one point in your code the array size is being equal to 1 and at that time you are trying to reffer to the position 1 (which is the location 2 since array starts at 0). This is giving this error.
for(index=1;index<=10;index++)
for(jndex=1;jndex<=10;jndex++)

{
uavArray[index][jndex] = new LinkedList();
uavArray[index][jndex].addFirst(new Integer(0));
}

I see a for loop in your code above. I suspect the problem is initiating from there since you are dynamically defining the array. I suggest you do a dry run on a paper which will be easy in this case to detect the error.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: LinkedList Posted: Sep 29, 2003 7:14 PM
Reply to this message Reply
I am posting it again since I used the [ code ] tag instead of [ java ] tag

Specifically it throws
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1


I think at one point in your code the array size is being equal to 1 and at that time you are trying to reffer to the position 1 (which is the location 2 since array starts at 0). This is giving this error.
for(index=1;index<=10;index++)
for(jndex=1;jndex<=10;jndex++)
 
{
uavArray[index][jndex] = new LinkedList(); 
uavArray[index][jndex].addFirst(new Integer(0)); 
} 

I see a for loop in your code above. I suspect the problem is initiating from there since you are dynamically defining the array. I suggest you do a dry run on a paper which will be easy in this case to detect the error.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: LinkedList Posted: Sep 30, 2003 12:06 AM
Reply to this message Reply
I didn't look too closely at the code (it is too painful -- try using the Java markup tags next time!), but based on what Senthoorkumaran showed, it looks like you are treating arrays as if they are 1-based (a la Pascal) instead of 0-based. In Java, you'd want to have loops that look like this:
   for( index = 0; index < 10; index++ )
      for( jndex = 0; jndex < 10; jndex++ )
         ...

Or even better, like this:
   for( int i = 0; i < firstArray.length; i++ )
      for( int j = 0; j < secondArray.length; j++ )
         ...

The advantage of this second approach is that if you later change your array sizes (or don't even know what their sizes are), the code will still work fine. The fewer "magic numbers" you have in your code (like 10, in this case), the better your code will be.

Subha

Posts: 16
Nickname: nikki
Registered: Sep, 2003

Re: LinkedList Posted: Oct 1, 2003 9:30 AM
Reply to this message Reply
Is my creation of the linkedlist right?what I am trying to do here is, create an array wherein each each cell of the array is a linked list.
i did do a dry run but i am not able to figure out the error.can you please help.

uavArray = new LinkedList[11][11];

for(index = 1;index <= uavArray.length; index++)
for(jndex = 1;jndex <= uavArray.length; jndex++)

{
uavArray[index][jndex] = new LinkedList();
uavArray[index][jndex].addFirst(new integer(0));
}
index=0;......//has to initialize the array only once.and not for each thread.

this is the error:
java.lang.ArrayIndexOutOfBoundsException
at ThreadHandler.newInit(uavmaze1.java:102)
at ThreadHandler.run(uavmaze1.java:349)
thanks a ton!

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: LinkedList Posted: Oct 1, 2003 11:31 AM
Reply to this message Reply
What part of "0-based" do you not understand?

Are you even reading the answers to your questions?

Please read the posts above. Your question has already been clearly and simply answered.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: LinkedList Posted: Oct 1, 2003 11:57 AM
Reply to this message Reply
Here's a quick tutorial on 0-based indexing:
If an array has 5 items, the indices of these items will be 0, 1, 2, 3, 4 and the length of the array will be 5. The last item in the array is at index 4 (or array.length - 1), not 5.

You increased your array size to 11, making it possible to index it (incorrectly) from 1 to 10 without a crash, but then you shot yourself in the foot by also using array.length, assuring that it will always fail, even if you increase the size to 12, 13, or all the way up to Integer.MAX_VALUE.

Using array.length was a good idea, but starting at 1 and comparing with "<=" instead of "<" are both bad ideas in Java. You want to start indexing at 0 and stop before you get to array.length.

Here's a little example you can experiment with. (By the way, since it looks like you are still just learning the fundamentals of Java, you might want to experiment with smaller programs to try and focus on specific concepts, so you don't get overwhelmed by lots of different things at once).

class arraystuff
{
   public static void main(String args[])
   {
      String [] array = {"Null","Ein", "Zwei", "Drei", "Fier" };
 
      System.out.println( "There are " + array.length + " items in the array:" );
 
      for( int i = 0; i < array.length; i++ )
         System.out.println( i + ". " + array[i] );
   }
}


Look closely and notice that indexing starts from 0 and stops before hitting array.length.

The output of this program will be:
There are 5 items in the array:
0. Null
1. Ein
2. Zwei
3. Drei
4. Fier


By the way, please use the java tags when posting code! It makes the code in the post much more readable. It is quite easy to do; here's an example:
[ java ]
for( int i = 0; i < array.length; i++ )
{
   ...
}
[ /java ]

Subha

Posts: 16
Nickname: nikki
Registered: Sep, 2003

Re: LinkedList Posted: Oct 1, 2003 2:15 PM
Reply to this message Reply
I am sorry to bother you with so many questions.Yes,I am new to java and I am still in the learning process.
I now understand what you were trying to tell me and I tried implemeting it in my program.

I am getting this error.Where so you think I am going wrong?Please help me.
Thanks


UAVs Starts to move
i am here
iam initialized
I am here2
UAV : 1 NE - Row :10 Col: 2

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.LinkedList.entry(Unknown Source)
at java.util.LinkedList.get(Unknown Source)
at ThreadHandler.UAVCoordinates(uavmaze1.java:204)
i am here
iam initialized
I am here2
UAV : 2 NE - Row :10 Col: 2

i am here
iam initialized
I am here2
UAV : 3 NE - Row :10 Col: 2

i am here
iam initialized
I am here2
UAV : 4 E - Row :1 Col: 2

i am here
iam initialized
I am here2
UAV : 5 E - Row :1 Col: 2

at ThreadHandler.run(uavmaze1.java:367)
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.LinkedList.entry(Unknown Source)
at java.util.LinkedList.get(Unknown Source)
at ThreadHandler.UAVCoordinates(uavmaze1.java:204)
at ThreadHandler.run(uavmaze1.java:367)
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.LinkedList.entry(Unknown Source)
at java.util.LinkedList.get(Unknown Source)
at ThreadHandler.UAVCoordinates(uavmaze1.java:204)
at ThreadHandler.run(uavmaze1.java:367)
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.LinkedList.entry(Unknown Source)
at java.util.LinkedList.get(Unknown Source)
at ThreadHandler.UAVCoordinates(uavmaze1.java:228)
at ThreadHandler.run(uavmaze1.java:367)
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.LinkedList.entry(Unknown Source)
at java.util.LinkedList.get(Unknown Source)
at ThreadHandler.UAVCoordinates(uavmaze1.java:228)
at ThreadHandler.run(uavmaze1.java:367)

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: LinkedList Posted: Oct 1, 2003 2:51 PM
Reply to this message Reply
Looks like more indexing problems. I don't have time to debug it for you. I suggest you step through your code with the debugger. Don't tell me you don't have one, either: NetBeans is free to download.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: LinkedList Posted: Oct 1, 2003 3:39 PM
Reply to this message Reply
Post your for loop segment again? or the entier code. But this time post it within the [ java ] [ /java ] so code is formatted

Subha

Posts: 16
Nickname: nikki
Registered: Sep, 2003

Re: LinkedList Posted: Oct 1, 2003 4:10 PM
Reply to this message Reply
I tried debugging my code.It says "Breakpoint reached at line 305 in class ThreadHandler by thread Thread-1."

also it points to a break point in the run method where in I have newInit();
I am pasting the code here again.
Thanks a lot for all ur help.
------------------------------
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;
 
class uavmaze1 {
    public static void main(String args[]) throws IOException {
        
        System.out.println("UAVs Starts to move");
        
        int i=1, delayCount;
        
        int initCount = 0;
        
        while (i!=6) {
            if(i==1) {
                ThreadHandler thread = new ThreadHandler(i,initCount);
                thread.start();
            }
            else {
                initCount++;
                ThreadHandler thread = new ThreadHandler(i,initCount);
                thread.start();
            }
            
            for(delayCount=0;delayCount<=170;delayCount++) {
                delayCount = delayCount++;
            }
            //increment i to label the next connection
            i++;
        }
        
    }
    
}
 
 
 
//define the thread class for handling new connection
class ThreadHandler extends Thread {
    private int number;            //label for the connection
    
    public LinkedList[][] uavArray;
    
    public int row, col;
    
    private int index,jndex;
    
    private int inCount;
    
    public int globalTime;
    Breakpoint reached at line 305 in class ThreadHandler by thread Thread-1.
 
    public int siz;
    
    public int a;
    
    public ThreadHandler(int id, int indexCount)
    
    {
        number = id;
        inCount = indexCount;
        row = 1;
        col = 1;
    }
    public void newInit() {
        //Declare and initialize the array
        
        uavArray = new LinkedList[11][11];
        
        for(index = 0 ;index < uavArray.length ; index++)
            for(jndex = 0 ; jndex<uavArray.length ; jndex++)
                
            {
                uavArray[index][jndex] = new LinkedList();
                uavArray[index][jndex].addFirst(new Integer(0));
            }
        System.out.println("iam initialized");
        
        index =0;
        
        //All points start from coordinate (1,1)
        
        row =1;
        
        col =1;
        
        globalTime= 1;
    }
    
    public void UAVCoordinates(int number, int posRandom) {
        
        //Generate Random number between 1 And 8
        
        globalTime++;
        
        
        switch (posRandom) {
            
            case 1:
                
                //Decrease both row and col -- NW
                
                if(row==1)
                    row=10;
                else
                    row = row - 1;
                
                if(col==1)
                    col=10;
                else
                    col = col - 1;
                
                
                System.out.println("UAV : " + number + " NW - Row :" + row + " Col: " + col + "\n");
                
                siz = (uavArray[row][col].size());
                
                a = ((Integer)(uavArray[row][col].get(siz))).intValue();
                
                uavArray[row][col].addLast(new Integer(globalTime -a));
                
                break;
                
            case 2:
                
                
                //Decrease row -- North
                if(row==1)
                    row=10;
                else {
                    row = row - 1;
                    col = col;
                }
                
                
                System.out.println("UAV : " + number + " N - Row :" + row + " Col: " + col + "\n");
                
                siz = (uavArray[row][col].size());
                
                a = ((Integer)(uavArray[row][col].get(siz))).intValue();
                
                uavArray[row][col].addLast(new Integer(globalTime -a));
                
                
                break;
                
            case 3:
                
                //Decrease row and Increase col - NE
                if(row==1)
                    row=10;
                else
                    row = row - 1;
                
                if(col==10)
                    col = 1;
                else
                    col = col + 1;
                
                
                System.out.println("UAV : " + number + " NE - Row :" + row + " Col: " + col + "\n");
                siz = (uavArray[row][col].size());
                
                a = ((Integer)(uavArray[row][col].get(siz))).intValue();
                
                uavArray[row][col].addLast(new Integer(globalTime -a));
                
                break;
                
            case 4:
                
                //Increase col - East
                
                if(col==10)
                    col = 1;
                else
                    col = col + 1;
                
                
                
                System.out.println("UAV : " + number + " E - Row :" + row + " Col: " + col + "\n");
                
                siz = (uavArray[row][col].size());
                
                a = ((Integer)(uavArray[row][col].get(siz))).intValue();
                
                uavArray[row][col].addLast(new Integer(globalTime -a));
                
                break;
                
            case 5:
                
                //Increase Row And Col -- SE
                
                if(col==10)
                    col = 1;
                else
                    col = col + 1;
                
                if(row==10)
                    row = 1;
                else
                    row = row + 1;
                
                
                
                System.out.println("UAV : " + number + " SE - Row :" + row + " Col: " + col + "\n");
                
                siz = (uavArray[row][col].size());
                
                a = ((Integer)(uavArray[row][col].get(siz))).intValue();
                
                uavArray[row][col].addLast(new Integer(globalTime -a));
                
                break;
                
            case 6:
                
                // Increase row -- South
                
                if(row==10)
                    row = 1;
                else
                    row = row + 1;
                
                
                
                System.out.println("UAV : " + number +  " S - Row :" + row + " Col: " + col + "\n");
                
                siz = (uavArray[row][col].size());
                
                a = ((Integer)(uavArray[row][col].get(siz))).intValue();
                
                uavArray[row][col].addLast(new Integer(globalTime -a));
                
                
                break;
                
            case 7:
                
                //Increase row and Decrease Col -- SW
                
                if(row==10)
                    row=1;
                else
                    row = row + 1;
                
                if(col==1)
                    col=10;
                else
                    col = col - 1;
                
                
                
                System.out.println("UAV : " + number +  " SW - Row :" + row + " Col: " + col + "\n");
                
                siz = (uavArray[row][col].size());
                
                a = ((Integer)(uavArray[row][col].get(siz))).intValue();
                
                uavArray[row][col].addLast(new Integer(globalTime -a))  ;
                break;
                
            case 8:
                
                //Decrease col -- W
                
                if(col==1)
                    col=10;
                else
                    col = col + 1;
                
                
                
                System.out.println("UAV : " + number + " W - Row :" + row + " Col: " + col + "\n");
                
                siz = (uavArray[row][col].size());
                
                a = ((Integer)(uavArray[row][col].get(siz))).intValue();
                
                uavArray[row][col].addLast(new Integer(globalTime - a));
                
                break;
                
            default:
                
        }
        
        
    }
    
    public void run() {
        
        int inCount=0,i,j;
        while(inCount!=1000) {
            if(inCount==0) {
                inCount=1;
                newInit();
            }
            
            
            Random generator = new Random();
            
            int posRandom = generator.nextInt(7) + 1;
            
            UAVCoordinates(number,posRandom);
            
            inCount++;
            
        }
        
        
        System.out.println("\n\n");
        
        System.out.println("*******************************************************");
        for(i = 0 ; i < uavArray.length ; i++)
            for(j = 0; j <uavArray.length ; j++)
                System.out.println("No of visits to location ( " + i + "," + j + ") >> " + uavArray[i][j] + "\n");
        
/*       System.out.println("*******************************************************");
     for(i=1;i<=10;i++)
       for(j=1;j<=10;j++)
 
          {
            System.out.println("Visit Time interval for each cell is >> \n");
            for ( int k = 0; k < linklist[i][j].size(); k++ )
               System.out.println( "Row = " + i + " And Col = " + j + "" + linklist[i][j].get( k )  + "\2t" );
             }*/
        
    } //run closes
    
} //class threadhandler closed

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: LinkedList Posted: Oct 2, 2003 3:21 AM
Reply to this message Reply
One thing I noticed in from you logs are that everytime your array seems to be getting initialised. Is that what you want? I was thinking in a maze you initialise it once and then travel it? You might want to consider removing the initialisation out of the thread?

Flat View: This topic has 26 replies on 2 pages [ 1  2 | » ]
Topic: Problem with CallableStatement and PreparedStatement - Urgent Previous Topic   Next Topic Topic: ?? Tool that produce Exe from Japa application ??

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use