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.

Results 1 to 2 of 2

Thread: Merging Arrays Vs. Linked List.

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    18
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Merging Arrays Vs. Linked List.

    I'm not familiar with LinkedList

    Can someone tell me how I can go about doing it ?

    Assuming we have the following constraints,
    two int linked list;
    and a third one to merge them into;

    Also, can a linked list be sorted the same way a array can be sorted?

    public static int[] MergeArrays(int[] ArrayA, int[] ArrayB)
      {
     
          int ArrayAindex = ArrayA.length;
          int[] MergedArray = new int[ArrayA.length + ArrayB.length];
     
          for (int x = 0; x < ArrayAindex; x++)
          {
              MergedArray[x] = ArrayA[x];
          }
     
          int MergedIndex = ArrayA.length;
     
          for (int i = 0; i < ArrayB.length; i++)
          {
            MergedArray[MergedIndex] = ArrayB[i];
            MergedIndex++;
          }
     
          return MergedArray;
      }


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Merging Arrays Vs. Linked List.

    Something like this.

            final List<Integer> moreIntegers = new LinkedList<Integer>();
            moreIntegers.add(3);
            moreIntegers.add(4);
     
            final List<Integer> integers = new LinkedList<Integer>();
            integers.add(1);
            integers.add(2);
     
            integers.addAll(moreIntegers);
     
            System.out.println(integers);

    We create two linked lists (integers and moreIntegers). We then add two numbers in moreIntegers and two numbers into integers. After that we add all items in moreIntegers into integers using addAll method.

    There you go, two linked lists merged together.

    // Json

Similar Threads

  1. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 03:33 AM
  2. circular linked list
    By student123xyz in forum Collections and Generics
    Replies: 4
    Last Post: August 19th, 2009, 10:40 AM
  3. ClassCastException in Double Linked List toString
    By Rastabot in forum Collections and Generics
    Replies: 2
    Last Post: April 24th, 2009, 11:48 AM
  4. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM
  5. String substring, concatenation operation on linked list
    By oaks12 in forum Collections and Generics
    Replies: 1
    Last Post: January 15th, 2009, 07:12 AM