Hack Forums
[Java] Opinion needed - Array List or Linked List - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Opinion needed - Array List or Linked List (/showthread.php?tid=2634967)



Opinion needed - Array List or Linked List - starvinmarvin1 - Jun 30, 2012

So basically I need to make a program which stores cd information and I need to write it to file.I need to be able to:

Add,
Edit or
Delete the information

So I used the Array List but when written to file the contents are a little shoddy.

I need your opinion will it be better to stay with the array list or maybe go with the linked list?


* - anuvab1911 - Jun 30, 2012

Guessing from your requirements,it would be better to implement your code through a LinkedList.

http://chaoticjava.com/posts/linkedlist-vs-arraylist/


* - starvinmarvin1 - Jun 30, 2012

(Jun 30, 2012 10:32 AM)anuvab1911 Wrote:  Guessing from your requirements,it would be better to implement your code through a LinkedList.

http://chaoticjava.com/posts/linkedlist-vs-arraylist/

Thanks for your input🤝. . .I was hoping to stay with the array list but I guess I should just stop being lazy


* - node.js - Jun 30, 2012

Wait, How are you storing the cd information? Is this a String ArrayList?


* - starvinmarvin1 - Jun 30, 2012

(Jun 30, 2012 10:53 AM)SoFlawLess_ Wrote:  Wait, How are you storing the cd information? Is this a String ArrayList?

Sorry I mention this, yes it is a String Array list


* - node.js - Jun 30, 2012

(Jun 30, 2012 10:54 AM)starvinmarvin1 Wrote:  Sorry I mention this, yes it is a String Array list

See that's the problem. Do something like this:

Code
import java.util.ArrayList;

public class CDManager {

    public static void main(String[] args) {
        ArrayList<CD> cds = new ArrayList<CD>();

        CD cd1 = new CD();
        cd1.artist = "SoFlawLess_";
        cd1.name = "Best Hits of SoFlawLess_";
        cds.add(cd1);

        CD cd2 = new CD();
        cd2.artist = "starvinmarvin1";
        cd2.name = "Awesome Hits of starvinmarvin1";
        cds.add(cd2);

        for (CD cd : cds) {
            System.out.println("CD Name: " + cd.name);
            System.out.println("CD Artist: " + cd.artist);
            System.out.println();
        }
    }
}

Code
public class CD {

    public String artist;
    public String name;

}



* - starvinmarvin1 - Jun 30, 2012

(Jun 30, 2012 11:02 AM)SoFlawLess_ Wrote:  See that's the problem. Do something like this:

Code
import java.util.ArrayList;

public class CDManager {

    public static void main(String[] args) {
        ArrayList<CD> cds = new ArrayList<CD>();

        CD cd1 = new CD();
        cd1.artist = "SoFlawLess_";
        cd1.name = "Best Hits of SoFlawLess_";
        cds.add(cd1);

        CD cd2 = new CD();
        cd2.artist = "starvinmarvin1";
        cd2.name = "Awesome Hits of starvinmarvin1";
        cds.add(cd2);

        for (CD cd : cds) {
            System.out.println("CD Name: " + cd.name);
            System.out.println("CD Artist: " + cd.artist);
            System.out.println();
        }
    }
}

Code
public class CD {

    public String artist;
    public String name;

}

You make Java seem easy bro. . .but how am I going to take lots of input ?

Thanks for your help🤝


* - anuvab1911 - Jun 30, 2012

Well,to take in lots of input,create a class CD like this as suggested by SoFlawLess_:

Code
public class CD{
  public String <info_field_0>;
  public String <info_field_1>;
  public String <info_field_2>;
  public CD(String i0,String i1,String i2){
  <info_field_0>=i0;
  <info_field_1>=i1;
  <info_field_2>=i2;
  }
}

Then create an ArrayList or LinkedList in your main class by the name say,cdList by writing it like:

Code
ArrayList<CD> cdList=new ArrayList<CD>();

Then,the function to add to the list will be something like:
Code
public static insertToList(String artist,String name,String date){
  CD cdNew=new CD(artist,name,date);
  cdList.add(cdNew);
}



* - starvinmarvin1 - Jul 1, 2012

Thanks for your help guys.

Got it sorted


* - Jire - Jul 1, 2012

(Jun 30, 2012 11:02 AM)SoFlawLess_ Wrote:  See that's the problem. Do something like this:

Code
import java.util.ArrayList;

public class CDManager {

    public static void main(String[] args) {
        ArrayList<CD> cds = new ArrayList<CD>();

        CD cd1 = new CD();
        cd1.artist = "SoFlawLess_";
        cd1.name = "Best Hits of SoFlawLess_";
        cds.add(cd1);

        CD cd2 = new CD();
        cd2.artist = "starvinmarvin1";
        cd2.name = "Awesome Hits of starvinmarvin1";
        cds.add(cd2);

        for (CD cd : cds) {
            System.out.println("CD Name: " + cd.name);
            System.out.println("CD Artist: " + cd.artist);
            System.out.println();
        }
    }
}

Code
public class CD {

    public String artist;
    public String name;

}
Reveal the behavior through an interface (for example, List<?>) rather than implementation (ArrayList<?>)

Also, I believe an array list is the better choice.