2012-11-27 14 views
13

संभव डुप्लिकेट:
How to concatenate two arrays in Java?जावा में दो सरणी में शामिल हों?

मुझे लगता है मैं दो वस्तुओं कैसे शामिल हो जाना चाहिए दो वस्तुओं

HealthMessage[] healthMessages1; 
HealthMessage[] healthMessages2; 

HealthMessage[] healthMessagesAll; 

healthMessages1 = x.getHealth(); 
healthMessages2 = y.getHealth(); 

है, तो मैं केवल एक लौट सकते हैं:

return healthMessagesAll; 

अनुशंसित तरीका क्या है?

+0

कोशिश [इस] (http://stackoverflow.com/questions/1978933/a-quick-and-easy-way-to-join-array जाना होगा -elements-with-a-separator-the-oposite-of-spl) –

+0

आप डुप्लिकेट को कैसे संभालना चाहते हैं? कहें Array1 में Array2 पर विचार किए बिना 3 डुप्लिकेट हैं, तो परिणामस्वरूप सभी 3 डुप्लिकेट परिणामस्वरूप उपलब्ध हो सकते हैं? – Jayy

+0

अच्छा सवाल Kaipa, डुप्लिकेट के बारे में नहीं सोचा था। – Danijel

उत्तर

31

अपाचे कॉमन्स collecion API का उपयोग करना:

healthMessagesAll = ArrayUtils.addAll(healthMessages1,healthMessages2); 
+3

+1 में वर्णित है। यदि आप अपाचे कॉमन्स का उपयोग कर सकते हैं तो अधिक सुरुचिपूर्ण। –

+0

इसे इस तरह से संपन्न किया। धन्यवाद। – Danijel

19

मैं healthMessages1 और healthMessages2 की कुल लंबाई के साथ एक सरणी का आवंटन और उनकी सामग्री की प्रतिलिपि करने के System.arraycopy या दो for छोरों का उपयोग करेंगे। है एक अच्छा तरीका

public class HelloWorld { 

    public static void main(String []args) { 

     int[] a = new int[] { 1, 2, 3}; 
     int[] b = new int[] { 3, 4, 5}; 
     int[] r = new int[a.length + b.length]; 
     System.arraycopy(a, 0, r, 0, a.length); 
     System.arraycopy(b, 0, r, a.length, b.length); 

     // prints 1, 2, 3, 4, 5 on sep. lines 
     for(int x : r) { 
      System.out.println(x); 
     }    
    }   
} 
0

सरणी लंबाई तय कर रहे हैं ताकि आप विभिन्न विकल्प है, यहाँ System.arraycopy के साथ एक नमूना है। यहां एक जोड़े हैं:

ए) दूसरों के आकार के साथ एक नई सरणी बनाएं और सभी तत्वों को मैन्युअल रूप से कॉपी करें।

healthMessagesAll = new HealthMessage[healthMessages1.length + healthMessages2.length]; 
int i = 0; 
for (HealthMessage msg : healthMessases1) 
{ 
    healthMessagesAll[i] = msg; 
    i++; 
} 

for (HealthMessage msg : healthMessages2) 
{ 
    healthMessagesAll[i] = msg; 
    i++; 
} 

ख) Arrays वर्ग द्वारा प्रदान की विधियों का उपयोग करें। आप सरणी को एक सूची में परिवर्तित कर सकते हैं, या थोक में चारों ओर तत्वों की प्रतिलिपि बना सकते हैं। इसे प्रदान किए गए कार्यों पर नज़र डालें और जो आपको उपयुक्त बनाता है उसे चुनें।

अद्यतन

डुप्लिकेट के बारे में अपनी टिप्पणी को देखकर। आप सब कुछ Set में रखना चाहते हैं जो विशिष्टता की गारंटी देता है। यदि आप दो बार एक ही तत्व जोड़ते हैं, तो इसे दूसरी बार जोड़ा नहीं जाएगा। यदि आप स्पष्ट रूप से अपने toArray() विधि के साथ सरणी की आवश्यकता होती है तो आप सेट को सरणी में वापस कनवर्ट कर सकते हैं।

अन्य उत्तरदाताओं द्वारा सुझाए गए अनुसार, System.arraycopy() आपको तत्वों की सामग्री की प्रतिलिपि बनाने में भी मदद करता है, इसलिए यह मेरे वैकल्पिक (ए) का एक छोटा संस्करण है।

0

क्या इस रास्ते में कुछ के बारे में:

List<String> l1 = Arrays.asList(healthMessages1); 
    l1.addAll(Arrays.asList(healthMessages2)); 
    HealthMessage[] result = l1.toArray(); 

(generification का एक सा की जरूरत है ... :)

13

यह और अधिक लिखने के लिए सहज ज्ञान युक्त है और आप सरणी अनुक्रमित से निपटने के लिए की जरूरत नहीं है :

Collection<HealthMessage> collection = new ArrayList<HealthMessage>(); 
collection.addAll(Arrays.asList(healthMessages1)); 
collection.addAll(Arrays.asList(healthMessages2)); 

HealthMessage[] healthMessagesAll = collection.toArray(new HealthMessage[] {}); 

.. लेकिन के बारे में यह System.arraycopy के विपरीत प्रदर्शन है मुझे मत पूछो।

+0

+1 एक अच्छा, साफ, आसान समाधान – sunil

+0

धन्यवाद, अच्छा जवाब। – Danijel

0

और सबसे जटिल लेकिन कम से कम स्मृति-भूख समाधान के लिए आप उन्हें किसी ऑब्जेक्ट में लपेट सकते हैं। यह एक नई सरणी में प्रतिलिपि बनाने के लिए सभी वस्तुओं में Iterator<T> और copyTo विधि प्रदान करता है। गेटर्स और सेटर्स प्रदान करने के लिए इसे आसानी से बढ़ाया जा सकता है।

public class JoinedArray<T> implements Iterable<T> { 
    final List<T[]> joined; 

    // Pass all arrays to be joined as constructor parameters. 
    public JoinedArray(T[]... arrays) { 
    joined = Arrays.asList(arrays); 
    } 

    // Iterate across all entries in all arrays (in sequence). 
    public Iterator<T> iterator() { 
    return new JoinedIterator<T>(joined); 
    } 

    private class JoinedIterator<T> implements Iterator<T> { 
    // The iterator across the arrays. 
    Iterator<T[]> i; 
    // The array I am working on. Equivalent to i.next without the hassle. 
    T[] a; 
    // Where we are in it. 
    int ai; 
    // The next T to return. 
    T next = null; 

    private JoinedIterator(List<T[]> joined) { 
     i = joined.iterator(); 
     a = nextArray(); 
    } 

    private T[] nextArray() { 
     ai = 0; 
     return i.hasNext() ? i.next() : null; 
    } 

    public boolean hasNext() { 
     if (next == null) { 
     // a goes to null at the end of i. 
     if (a != null) { 
      // End of a? 
      if (ai >= a.length) { 
      // Yes! Next i. 
      a = nextArray(); 
      } 
      if (a != null) { 
      next = a[ai++]; 
      } 
     } 
     } 
     return next != null; 
    } 

    public T next() { 
     T n = null; 
     if (hasNext()) { 
     // Give it to them. 
     n = next; 
     next = null; 
     } else { 
     // Not there!! 
     throw new NoSuchElementException(); 
     } 
     return n; 
    } 

    public void remove() { 
     throw new UnsupportedOperationException("Not supported."); 
    } 
    } 

    public int copyTo(T[] to, int offset, int length) { 
    int copied = 0; 
    // Walk each of my arrays. 
    for (T[] a : joined) { 
     // All done if nothing left to copy. 
     if (length <= 0) { 
     break; 
     } 
     if (offset < a.length) { 
     // Copy up to the end or to the limit, whichever is the first. 
     int n = Math.min(a.length - offset, length); 
     System.arraycopy(a, offset, to, copied, n); 
     offset = 0; 
     copied += n; 
     length -= n; 
     } else { 
     // Skip this array completely. 
     offset -= a.length; 
     } 
    } 
    return copied; 
    } 

    public int copyTo(T[] to, int offset) { 
    return copyTo(to, offset, to.length); 
    } 

    public int copyTo(T[] to) { 
    return copyTo(to, 0); 
    } 

    @Override 
    public String toString() { 
    StringBuilder s = new StringBuilder(); 
    Separator comma = new Separator(","); 
    for (T[] a : joined) { 
     s.append(comma.sep()).append(Arrays.toString(a)); 
    } 
    return s.toString(); 
    } 

    public static void main(String[] args) { 
    JoinedArray<String> a = new JoinedArray<String>(
      new String[]{ 
       "One" 
      }, 
      new String[]{ 
       "Two", 
       "Three", 
       "Four", 
       "Five" 
      }, 
      new String[]{ 
       "Six", 
       "Seven", 
       "Eight", 
       "Nine" 
      }); 
    for (String s : a) { 
     System.out.println(s); 
    } 
    String[] four = new String[4]; 
    int copied = a.copyTo(four, 3, 4); 
    System.out.println("Copied " + copied + " = " + Arrays.toString(four)); 

    } 
} 
+0

... अच्छा है, लेकिन इकाई परीक्षणों को मत भूलना ;-) – Kai

3

मैं साथ System.arraycopy

private static HealthMessage[] join(HealthMessage[] healthMessages1, HealthMessage[] healthMessages2) 
{ 
    HealthMessage[] healthMessagesAll = new HealthMessage[healthMessages1.length + healthMessages2.length]; 

    System.arraycopy(healthMessages1, 0, healthMessagesAll, 0, healthMessages1.length); 
    System.arraycopy(healthMessages2, 0, healthMessagesAll, healthMessages1.length, healthMessages2.length); 

    return healthMessagesAll; 
} 
संबंधित मुद्दे