← Back to Scala Course | Chapter 6: Collections Basics | Lesson 4 of 8

ArrayBuffer क्या है

ArrayBuffer boxes की एक लचीली पंक्ति है जो चीज़ें जोड़ने या हटाने पर बढ़ती या सिकुड़ती है। यह एक backpack जैसा है जो ज़्यादा सामान रखने के लिए फैल जाता है।

In this page:

  1. ArrayBuffer
Syntax
scala
import scala.collection.mutable.ArrayBuffer
val buffer = ArrayBuffer(value1, value2)
buffer += value
buffer -= value

ArrayBuffer

ArrayBuffer (scala.collection.mutable से) एक resizable, mutable sequence है, जो दूसरी languages में dynamic array या ArrayList जैसा है।

Array के विपरीत, यह element mutation की अनुमति देने के साथ-साथ += और -= से बढ़ या सिकुड़ भी सकता है। जब आपको किसी sequence को धीरे-धीरे बनाना हो तो यह पसंदीदा mutable collection है।

Note: जब collection का size बनने के बाद बदलना ज़रूरी हो, सिर्फ उसके element values नहीं, तब Array के बजाय ArrayBuffer का उपयोग करें।

उदाहरण: ArrayBuffer

markup
import scala.collection.mutable.ArrayBuffer

object Main extends App {
  val buffer = ArrayBuffer(1, 2, 3)
  buffer += 4
  buffer += 5

  println(buffer)
  println(buffer.length)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. import scala.collection.mutable.ArrayBuffer भूल जाना, जिससे type नहीं मिलता।
  2. कोई element जोड़ने के लिए += के बजाय + का उपयोग करना, जो current buffer बदलने के बजाय एक नया buffer बनाता है।
  3. किसी buffer पर loop चलाते हुए उसमें जोड़ना, जो अनपेक्षित results दे सकता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.