LinkedList tutor?

Hi, been learning about arrays lately, and is now switching to linked lists, however, the book I use has about 20 lines with linked lists (thats bad?), so is there any good tutorials out there? some that provide a working example, not just creating the list, but also using it to store data and manipulate it?
thanks for all tips! (and no, I dont realy want to buy another book, they are expensive!)

Hi, been learning about arrays lately, and is now
switching to linked lists, however, the book I use has
about 20 lines with linked lists (thats bad?),Not if it's a general Java book. Arrays are an integral part of the language whereas linked lists aren't. If you want to know more about them look for a book about algoritms and data structures. I thgink there are a few with Java as example language. And why don't you check out other important data structures too like trees and hash tables while you're at it. It's basically a must to know about them.

Similar Messages

  • How to retrieve the values from a LinkedList

    Hello,
    I have just put this question in java programming forums by mistake...I think that it should be here ...
    I have created a LinkedList to store the results of a query to a database.
    These reasults are decimal numbers and then I want to sum all these numbers to be able to make the average.
    But when I try to retrieve the values of the Linked List I always receive an incopatible types error..
    Here is an extract of my code in a jsp page.
    LinkedList Average = new LinkedList();
    String Media = rst.getString(10);
    Average.add(Media);
    int Size = Average.size();
    double Sum = 0.0;
    for (int i=0; i<=Size; i++)
                    double Result = Average.get(i)     
                  Sum = Sum + Result;
    }If I try to retrieve the value of only one node from the list , I can just putting <%=Average.get(i)%>...but..how can I retrieve all the values (they are decimal numbers) to be able to add them?

    If you want to sum all the values, is there any reason you just don't retrieve the sum from the database rather than the list of values?
    anyway
    List average = new LinkedList();
    while (rst.next()){
      // retrieve the number:
      String mediaString = rst.getString(10);
      Double media = Double.valueOf(mediaString);
      // or maybe like this if it is a number in the database
      Double media = new Double(rst.getDouble(10));
      average.add(media);
    doubleSum = 0.0;
    for (Iterator it = average.iterator(); it.hasNext(); ){
      Double result= (Double) it.next();
      doubleSum += result.doubleValue();
    }

  • Test in a LinkedList...

    if there is a way to know if my LinkedList is empty and the way to display the number of element(in this case "0").
    If you know how to code it could you show me the way...
    thanks

    Yes, there are methods for this. To display the number of elements:
    yourList.size();And to check if it's empty:
    yourList.isEmpty();That'll return true of false. The API can tell you all of this. Use it for future reference because it's all you need:
    http://java.sun.com/j2se/1.4/docs/api/java/util/LinkedList.html

  • Performance with LinkedList in java

    Hello All,
    Please suggest me any solution to further improve the performance with List.
    The problem description is as follows:
    I have a huge number of objects, lets say 10,000 , and need to store the objects in such a collection so that if i want to store an object at some particular index i , I get the best performance.
    I suppose as I need indexed based access, using List makes the best sense as Lists are ordered.
    Using LinkedList over ArrayList gives the better performance in the aforementioned context.
    Is there are way I can further improve the performance of LinkedList while solving this particular problem
    OR
    Is there any other index based collection using that i get better performance than LinkedList?
    Thanks in advance

    The trouble with a LinkedList as implemented in the Java libraries is that if you want to insert at index 100, it has no option but to step through the first 100 links of the list to find the insert point. Likewise is you retrieve by index. The strength of the linked list approach is lost if you work by index and the List interface gives no other way to insert in the middle of the list. The natural interface for a linked list would include an extended Iterator with methods for insert and replace. Of course LinkedLists are fine when you insert first or last.
    My guess would be that if your habitual insertion point was half way or more through the list then ArrayList would serve you better especially if you leave ample room for growth. Moving array elements up is probably not much more expensive, per element, than walking the linked list. Maybe 150% or thereabouts.
    Much depends on how you retrieve, and how volatile the list is. Certainly if you are a read-mostly situation and cannot use an iterator then a LinkedList won't suit.

  • How to print the content of LinkedList int[] and LinkedList LinkedList ?

    Hi guys, its been a long time since i posted here, and now im coming back to programming using java. My problem is, how can i print the content of the list?
    Example:
    LinkedList<int[]> list = new LinkedList<int[]>;
    int[] input = {1,2,3,4,5};
    int[] input2 = {2,32,43,54,65};
    list.add(input);
    list.add(input2);
    how can i print all the content of the linkedlist?
    Please help me..I know its a dumb question but i really dunno how.
    here is the code:
    import java.util.LinkedList;
    import java.util.Scanner;
    import java.util.Arrays;
    public class Test{
         static void printThis(String[] in){
              System.out.print("Value: ");
              for(int i = 0;i<in.length;i++){
                   System.out.print(in[i] + " ");
              System.out.println();
         static void reset(String[] val){
              for(int i = 0;i<val.length;i++){
                   val[i] = "";
         public static void main(String[] args){
              LinkedList<String[]> list = new LinkedList<String[]>();
              LinkedList<String> listTrans = new LinkedList<String>();
              System.out.print("Enter the number of records: ");
              Scanner s = new Scanner(System.in);
              int numOfRecords = s.nextInt();
              System.out.print("Enter the number of records per run: ");
              s = new Scanner(System.in);
              System.out.println();
              int numOfRecordsInMemory = s.nextInt();
              String[] getData = new String[numOfRecords];
              String[] transferData = new String[numOfRecordsInMemory];
              int numOfRuns = 0;
              int counter = 0;
              for(int i = 0;i<numOfRecords;i++){
                   counter++;
                   System.out.print("Enter value number " + counter + ": ");
                   Scanner scan = new Scanner(System.in);
                   getData[i] = scan.next();
                   listTrans.add(getData);
              if(getData.length%numOfRecordsInMemory == 0){
                   numOfRuns = getData.length/numOfRecordsInMemory;
              }else if(getData.length%numOfRecordsInMemory != 0){
                   numOfRuns =(int)(getData.length/numOfRecordsInMemory)+ 1;
              System.out.println();
              System.out.println("Number of Runs: " + numOfRuns);
         int pass = 0;
         System.out.println("Size of the main list: " + listTrans.size());
         while(listTrans.size() != 0){
              if(listTrans.size() >= numOfRecordsInMemory){
                   for(int i = 0;i<numOfRecordsInMemory;i++){
                        transferData[i] = listTrans.remove();
                   System.out.println("Size of the list: " + listTrans.size());
                   printThis(transferData);
                   System.out.println();
                   Arrays.sort(transferData);
                   list.add(transferData);
                   reset(transferData);
              }else if(listTrans.size() < numOfRecordsInMemory){
                   pass = listTrans.size();
                   for(int k = 0;k<pass;k++){
                        transferData[k] = listTrans.remove();
                   System.out.println("Size of the list: " + listTrans.size());
                   printThis(transferData);
                   System.out.println();
                   Arrays.sort(transferData);
                   list.add(transferData);
                   reset(transferData);
    //This is the part that is confusing me.
    //im trying to print it but its not working.
              System.out.println("Size of the next list: " + list.size());
    //          for(int i = 0;i<list.size();i++){
    //                    System.out.println();
    //               for(int j = 0;j<list.get(i)[j].length();j++){                    
    //                    System.out.print(list.get(i)[j] + " ");

    Here's the funnest, mabye clearest way you could do it: Use 2 Mappers
    package tjacobs.util;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import tjacobs.Arrays;
    public class Mapper <T>{
         public static interface MappedFunc<T> {
              void map(T value);
         public Mapper(T[] vals, MappedFunc<T> function) {
              this (new Arrays.ArrayIterator<T>(vals), function);
         public Mapper(Iterator<T> iterator, MappedFunc<T> function) {
              while (iterator.hasNext()) {
                   function.map(iterator.next());
         public static void main(String[] args) {
              String[] s = new String[] {"a","b", "c", "abc", "ab"};
              MappedFunc<String> func = new MappedFunc<String>() {
                   public void map(String s) {
                        if (s.toLowerCase().startsWith("a")) {
                             System.out.println(s);
              Mapper m = new Mapper(s, func);
    }

  • Error while loading .sim file with sap tutor 2.2

    Dear Gurus,
    I had recorded a tutorial which is am attempting to edit now.
    On opening the tutorial through the editor, I get the following message:
    "The following error occured while opening the file:
    error loading archive: no valid header or TOC found (80040002)"
    I am able to edit other tutorials satisfactorily.
    I seek guidance in resolving the error and opening the tutorial.
    Note: I downloaded SAP Tutor from SWDC.
    Regards,
    Ajay

    I was using the SAP Personal Player to play the tutorials. After I installed the licenced version from service.sap.com, I was able to open the .sim files.
    Thanks!

  • SAP Tutor error on Windows Vista

    Hello people
    We download the SAP Tutor player (https://websmp206.sap-ag.de/~sapidp/011000358700001571672002E/PERSONAL_INSTALL.EXE) and install it, when we try to use it close unexpectedly for windows vista.
    Somebody knows the cause of this error or knows if already the Vista version  exists and where we can download it ?
    Thanks

    Hi all,
    I tried to install the new SAP Tutor 2.1 in Vista but at the beginning the installation abort with error 2738. I found that this was related to some scripting problem, so I solved it executing from command prompt as administrator the following command:
    <b>regsvr32 vbscript.dll</b>
    After this I could do a full install without problems.
    Then when trying to play a sim file the player abort also, this time I could apply the solution discussed here (<b>compatibility property set to XP SP2</b>). I have to do that not only for the player but also for the rest of the SAP Tutor tools. In particular, I had to rise the privilege level to administrator when executing the SAPTutorSwitch.
    HTH somebody else.
    Kind regards.

  • Tutor 14 with Windows 8.1 and MS Office 2013

    Hi all,
    Have anybody used Tutor Author and Publisher with Windows 8.1 and MS Office 2013? Are there any known issues/compatibility issues with this? Any information on this is greatly appreciated.
    thanks in advance,
    SD

    That would be great! I would really appreciate if you can try this. I am having trouble creating the master index and desk manual index from tutor Publisher. I have tried with multiple machines but i am running into winkey error. I am already working with oracle for this, but there has been no progress. Kindly update once you are done testing or if you are able to use it without any issues.
    thanks in advance,
    SD

  • Iterating performance: ArrayList, LinkedList, and Hashmap for N 1,000,000

    I have seen some websites discussing about the performance of ArrayLists, LinkedLists, and Hashmaps:
    http://forum.java.sun.com/thread.jspa?threadID=442985
    http://java.sun.com/developer/JDCTechTips/2002/tt0910.html
    http://www.javaspecialists.co.za/archive/Issue111.html
    http://joust.kano.net/weblog/archives/000066.html
    If I understand it right an ArrayList in general is faster for accessing a particular element in the collection and I can't find some pro's of using a LinkedList.
    My question is: If I only use a large collection with more than 1 million elements for iterating from begin to end (i.e. for loop), is it faster to use a linked list or a custom linked list instead of an arraylist (or hashmap)? Since you can iterate "directly" through a (custom) linked list, which is not possible with a arraylist?
    Edited by: 9squared on Nov 23, 2007 1:48 PM

    Thanks for the help, I wrote some code and tested it
    import java.util.ArrayList;
    import java.util.List;
    import java.util.LinkedList;
    public class TestTemp {
         public static void main(String[] args) {
              List<Node> a = new ArrayList<Node>();
              Node b = new Node("a");
              String[] c = new String[10000000];
              Node temp = b;
              for (int i = 0; i < 10000000; i++)
                   a.add(new Node("a"));
                   temp.next = new Node("b");
                   temp = temp.next;     
                   c[i] = "c";
              long tstart;
              tstart = System.currentTimeMillis();
              for (int i = 0; i < 10000000; i++)
                   c[i] = "cc";
                   if (i%200000 == 0)
                        System.out.println("Array " + i + ": " + (System.currentTimeMillis()-tstart));
              tstart = System.currentTimeMillis();
              temp = b;
              for (int i = 0; i < 10000000; i++)
                   temp.next.text = "bb";
                   temp = temp.next;
                   if (i%200000 == 0)
                        System.out.println("LinkedList " + i + ": " + (System.currentTimeMillis()-tstart));
              tstart = System.currentTimeMillis();
              for (int i = 0; i < 10000000; i++)
                   a.get(i).text = "aa";
                   if (i%200000 == 0)
                        System.out.println("ArrayList " + i + ": " + (System.currentTimeMillis()-tstart));
    public class Node {
         public String text;
         public Node next;
         public Node(String text)
              this.text = text;
    }Here are some results in milliseconds, and indeed just iterating doesn't take very long
    Elements     Linked     Arraylist     Array
    200000     0     0     1
    400000     5     13     5
    600000     9     22     9
    800000     14     32     12
    1000000     20     42     16
    1200000     25     52     19
    1400000     31     63     23
    1600000     37     72     26
    1800000     42     82     30
    2000000     47     92     33
    2200000     51     101     37
    2400000     56     112     40
    2600000     60     123     44
    2800000     65     134     47
    3000000     69     143     51
    3200000     73     152     55
    3400000     78     162     59
    3600000     84     175     63
    3800000     103     185     67
    4000000     108     195     70
    4200000     113     207     74
    4400000     117     216     78
    4600000     122     225     81
    4800000     127     237     85
    5000000     131     247     88
    5200000     136     256     92
    5400000     142     266     97
    5600000     147     275     101
    5800000     153     286     107
    6000000     159     298     113
    6200000     162     307     117
    6400000     167     317     121
    6600000     171     326     125
    6800000     175     335     128
    7000000     180     346     132
    7200000     184     358     136
    7400000     188     368     139
    7600000     193     377     143
    7800000     197     388     147
    8000000     201     397     150
    8200000     207     410     154
    8400000     212     423     157
    8600000     217     432     162
    8800000     222     442     167
    9000000     227     452     171
    9200000     231     462     175
    9400000     236     473     178
    9600000     242     483     182
    9800000     249     495     185
    10000000     257     505     189

  • Looking for a Tutor to teach me Photoshop CS2

    I am looking for someone who can spend from 1-3 hours, in 30 minute to 1 hr intervals, teaching me some key areas of Photoshop CS2. The person needs to be in th4e United States and speak english fluently without a heavy foreign accent. I am looking for step by step, live tutoring over the phone. I have tried to use the photoshop training materials but I still don't "get" it. At a minimum, I want to learn, in detail, the following:
    -learn how to extract objects from images
    -learn how to change a certain color in an image to another color
    -understand layers and how to break an image down into layers
    -learn how to create masks and work with layers.
    I will pay by the hour - hoping to get someone around 30.00/hr.
    thanks
    Patrick

    no so much a dig at the OP as just an observation. it totally confounds me why someone would pay for what they already have. learn the terminology and what the app is capable of first by reading the manual (ANY manual, this doesn't just apply to learning photoshop), then when you can speak the lingo, you can really get your $20/hr worth of instruction because you'll be better equipped to ask relevant questions.
    the axiom: there's no such thing as a stupid question applies only as far as you're not paying for answers.

  • Need to find the source code for java.util.LinkedList

    Hello all,
    I'm new to the forums and I actually need the LinkedList source file as well as all the source files for all Java packages and libraries. Please post a link here if you can, or email me at [email protected]
    Thanks again!
    copter_man

    But those must be platform dependant, not really that
    interesting to look at. I have never felt any need to
    look at those at least.Wake up and smell the coffee, abbie! I read the source code all the time and constantly tell my students to do that, too.
    1. Java source code is not platform dependent. The original poster wanted to look at linked list code, how could that be platform dependent?
    2. It is the implementation of the SDK APIs, so by definition it is implementation dependent code, liable to change in a later version. So don't assume anything beyond what is claimed in the API documentation. However, sometimes the documentation is incomplete or ambiguous to you, and reading the source can provide some insight, however implementation dependent. (Light a candle or curse the darkness.)
    3. Why read source code? It's a good way to learn how to program. You see something in the API and ask: how'd they do that? Or you realize you want to do something broadly similar, but you can't reuse the source code.
    For example, Images are not Serializable, but suppose you want a small image that is part of an object to be serialized with the rest of the object without too much fuss (ie, without using javax.imageio). You notice ImageIcon is serializable, so you use it. (An ImageIcon has-an Image.) Then you wonder: how'd they do that? You read the source and then you know.
    You live, you learn,
    Nax

  • Publishing .doc files with Tutor Publisher 14

    All of my documents have been created in Tutor Author 14 and Word 2003, thus they have the extension .doc. However when I try to publish them in Tutor Publisher 14 at the 'Rebuild Master Index' stage no files are found. Tutor isn't recognizing the .doc files as valid files even though in the previous step (File>Publish step) it had moved all the .doc files into the 'final' folder.
    Does anyone have suggestions on how to fix this problem?
    Thanks

    In the Publisher > Tools > Configuration Options dialog box, what version of word is selected?
    For Publisher to find and bild the index against doc files, you must have 03 selected as the word version.
    If you do have 03 selected and Publisher still shows 0 files, please log an SR with Oracle Support.

  • For each loop and modification of a LinkedList

    When going thourhg my LinkedList with a for each loop, I want to do a modification. I just saw that it cast a ConcurrentModification Exception, so it seems I can't do that.
    How should I do then? I don't want to go through all the list each time an object is removed, I want it to restart where it stopped.

    Not the best solution but something like thisshould work:
    if( linkedList.get(i) == "modification")No: don't compare Strings with the == operator.
    http://access1.sun.com/FAQSets/newtojavatechfaq.html#9
    You are correct, must be .equals() my bad.

  • Array of linkedlist of object - help! plesse!

    I am writing an array of linkedlist and the elements of the linkedlist is an object carrying 2 interger. It didn't compile and the message is as follows:
    C:\thesis5>javac testLL2.java
    testLL2.java:22: cannot resolve symbol
    symbol : variable vertexFanObj2
    location: class testLL2
    vertexFanObj2 = myLL2[ i].get(j);
    ^
    1 error
    My program is as follows:
    import java.net.*;
    import java.io.*;
    import java.util.LinkedList;
    public class testLL2 {
    public static void main(String[] args) throws IOException {
    int myLLLength;
    Integer intObj;
    LinkedList myLL2 [] = new LinkedList[10];
    vertexFan vertexFanObj;
    for (int i = 0; i < 10; i++) {
    myLL2[ i] = new LinkedList();
    for (int j=0; j < 5; j++) {
    vertexFanObj = new vertexFan();
              vertexFanObj.setupVertex(j, -j);
    myLL2.add(vertexFanObj);
    for (int i = 0; i < 10; i++) {
    myLLLength = myLL2[ i].size();
    for (int j=0; j<myLLLength; j++) {
    vertexFanObj2 = myLL2[ i].get(j);
    System.out.println(vertexFanObj.vertex1+" "+vertexFanObj.vertex2);
    class vertexFan {
    int vertex1, vertex2;
    void setupVertex(int vertexA, int vertexB) {
    vertex1=vertexA;
    vertex2=vertexB;
    I 've got lost! Please kindly help you! Many thanks in advance!

    for (int i = 0; i < 10; i++) {
    myLL2[ i] = new LinkedList();
    for (int j=0; j < 5; j++) {
    vertexFanObj = new vertexFan();
    vertexFanObj.setupVertex(j, -j);
    myLL2.add(vertexFanObj);
    for (int i = 0; i < 10; i++) {
    myLLLength = myLL2[ i].size();
    for (int j=0; j<myLLLength; j++) {
    vertexFanObj2 = myLL2[ i].get(j);
    System.out.println(vertexFanObj.vertex1+" "+vertexFanObj.vertex2);
    } Its a scope issue.
    You define vertexFanObj in a for loop. then try to access it from outside that for loop. (in another for loop)
    for (int i = 0; i < 10; i++) {
    myLL2[ i] = new LinkedList();
    vertexFanObj = new vertexFan(); //I MOVED THIS LINE
    for (int j=0; j < 5; j++) {
    vertexFanObj.setupVertex(j, -j);
    myLL2.add(vertexFanObj);
    for (int i = 0; i < 10; i++) {
    myLLLength = myLL2[ i].size();
    for (int j=0; j<myLLLength; j++) {
    vertexFanObj2 = myLL2[ i].get(j);
    System.out.println(vertexFanObj.vertex1+" "+vertexFanObj.vertex2);
    } should work properly...

  • Converting a linkedlist to an array

    I currently have a program which reads in values from a file and stores them in a linked list, but i would like to convert this linked list to an array so i can use my bubble sort code on it which ive already got and it works by sorting through an array. i've tried using the .toArray() method on the list to convert it to an array but its not working. can anyone help me out here?

    try something like this:
    LinkedList list = new LinkedList();
    ... // populating the list
    Object[] listItems = (Object[])list.toArray(new Object[0]);you should replace Object[] by the Class type you are going to resolve (String[], Integer[], SomethingElse[], ...).

Maybe you are looking for

  • Arraylist only reading first line of file

    I am reading a text file using an arraylist. my file has a single value on each line e.g. 2 3 4 5 however this method is only reading only the first line of each file. is it because of this statement: String s1[] = number.split("\\s"); I would like t

  • I have a white intel i mac and when i boot up a file with a question mark shows up

    i installed wnow leopard and then attempted to use boot camp to install windows (thats a whole other story though) after i got windows on it and then got it back to the mac side i found i could not get back to the windows side so i attemted to delete

  • Significant of key field in Info cube table

    Hi, what is significant of key field in Info cube table? Best regards, dushyant.

  • FCPX Squirrely Chipmunk Audio

    I have a feature length documentary that sporadically but maddingly has the audio of random clips suddenly sound like Alvin & The Chipmunks, requiring me to delete the offending clip & reimport from original media. Anyone know what causes this & ways

  • Best way to collect periodic data over RS232?

    I'm using Labview 6i to collect data from a device over RS232.  The device manual says that it delivers data digitally by sending 10 bytes every 100ms.  I'm using VISA Write to send an initialization string, and then I step in to a loop containing VI