Array-Werte an anderes Array übergeben, nicht nur Referenz - VB-Paradise 2.0 – Die große Visual-Basic- und .NET-Community (2024)

  • C#
  • .NET (FX) 4.5–4.8

Es gibt 12 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    11. Juni 2014, 10:48

    Hallo,

    ich möchte gerne ein Array 1:1 an eine andere Variable übergeben. Dabei soll allerdings nicht nur eine Referenz übergeben werden sondern die Werte.

    Da Array ein Referenz-Typ ist, klappt es nach folgender Methode nicht:

    C#-Quellcode

    1. int[,] array = new int[5, 5];
    2. int z = 0;
    3. for (int x = 0; x < array.GetLength(0); x++)
    4. {
    5. for (int y = 0; y < array.GetLength(1); y++)
    6. {
    7. array[x, y] = z++;
    8. }
    9. }
    10. int[,] array2 = array;

    11. Juni 2014, 10:50

    Deep Copy von einer List(of)

    11. Juni 2014, 10:56

    Array.Copy kopiert die Elemente:

    VB.NET-Quellcode

    1. Sub Main()
    2. Dim arr(5, 5) As Integer
    3. Dim z As Integer = 0
    4. For x As Integer = 0 To arr.GetLength(0) - 1
    5. For y As Integer = 0 To arr.GetLength(1) - 1
    6. arr(x, y) = z
    7. z += 1
    8. Next
    9. Next
    10. Dim arr2(5, 5) As Integer
    11. Array.Copy(arr, arr2, arr.Length)
    12. arr2(0, 0) = 999999
    13. arr(0, 0) = 88888
    14. Debug.Print(arr(0, 0).ToString())
    15. Debug.Print(arr2(0, 0).ToString())
    16. Console.Read()
    17. End Sub

    lg

    ScheduleLib 0.0.1.0
    Kleine Lib zum Anlaufen von Code zu bestimmten Zeiten

    Danke! Was ist denn der Unterschied zwischen Clone und CopyTo und was ist sinnvoller?

    C#-Quellcode

    1. int[,] array = new int[5, 5];
    2. int z = 0;
    3. for (int x = 0; x < array.GetLength(0); x++)
    4. {
    5. for (int y = 0; y < array.GetLength(1); y++)
    6. {
    7. array[x, y] = z++;
    8. }
    9. }
    10. int[,] array2 = (int[,])array.Clone();

    EDIT: Ach so, habe übersehen, dass das VB ist. Ist also Clone in C# das Äquivalent zu Array.Copy in VB.net?

    11. Juni 2014, 11:05

    WhitePage schrieb:

    Ach so, klar, bei Integer kann man einfach zuweisen. Nur bei Referenzwerten würde es nicht gehen.

    Ein Integer-Array ist aber auch ein Referenztyp.

    WhitePage schrieb:

    Für Zahlen reicht das Clone .

    Wieso reicht das nur bei Zahlen?

    11. Juni 2014, 11:08

    Ein Array ist zwar ein Referenztyp, aber seine Bestandteile sind Wertetypen. Und dann funktioniert das Klonen auch.
    Wenn du Referenztypen in deinem Array hast (Objekte), dann werden nur die Referenzen dieser Objekte kopiert.
    Steht doch alles in meinem Link.

    11. Juni 2014, 11:10

    Es muss in C# genau so die .Copy Methode zur Verfügung stehen.
    Problem was sein kann, dass du das Array "array" nennst. Würde da einen anderen Namen wählen da es ja auch eine Klasse Array mit gibt.

    lg

    ScheduleLib 0.0.1.0
    Kleine Lib zum Anlaufen von Code zu bestimmten Zeiten

    11. Juni 2014, 11:28

    Wenn sourceArray und destinationArray beide Referenztyparrays oder Arrays vom Typ Object sind, wird eine flache Kopie erstellt. Einer flache Kopie eines Array ist ein neues Array, das Verweise auf dieselben Elemente wie das ursprüngliche Array enthält. Die eigentlichen Elemente sowie Elemente, auf die ggf. verwiesen wird, werden nicht kopiert. Im Gegensatz dazu werden bei einer tiefen Kopie eines Array die Elemente sowie alle untergeordneten Elemente kopiert, auf die direkt oder indirekt verwiesen wird.

    msdn.microsoft.com/de-de/library/k4yx47a1(v=vs.110).aspx

    11. Juni 2014, 20:41

    Gefragt ist hier nach einem Array-Klon. (tiefe Kopie eines Referenz-Typen heißt "Klon")
    Array implementiert auch ICloneable, also wäre das Thema damit eiglich erledigt. Nur die ICloneable-Schnittstelle returnt idiotischerweise Datentyp Object, und man muss den Klon also noch auf den Original-Typ casten.

    VB.NET-Quellcode

    1. dim arr= {1,2,3,4}
    2. dim clone =DirectCast(arr.Clone, Integer())

    Wers einfacher haben will kann auch einfach Linq hernehmen:

    VB.NET-Quellcode

    1. dim clone=arr.ToArray

    das klont auch.

    Grundlagen: Fachbegriffe

    11. Juni 2014, 20:51

    ErfinderDesRades schrieb:

    tiefe Kopie eines Referenz-Typen heißt "Klon"

    In meinem MSDN-Link im Post#5 steht es, dass eine flache Kopie dabei erstellt wird.

    11. Juni 2014, 21:05

    ups - ja - flache Kopie.
    Tief wäre, wenn die Array-Elemente Referenz-Typen wären, und auch diese geklont würden. Tut bei Integer-Array nix zur Sache, denn Integers sind Structures, die werden immer kopiert.

    12. Juni 2014, 07:04

    ErfinderDesRades schrieb:

    denn Integers sind

    "Value Types", nicht aber "Reference Types". Jou.

    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch Array-Werte an anderes Array übergeben, nicht nur Referenz - VB-Paradise 2.0 – Die große Visual-Basic- und .NET-Community (1)
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

  • Ähnliche Themen

    • Userform eine Variable als "Referenz" mit geben

      SoEinVBler- - Sonstige Problemstellungen

    • image class speicherbelastung

      vanao- - Sonstige Problemstellungen

    • Array in Hashtable ?

      Zim- - Sonstige Problemstellungen

    • Array teils löschen, andere nachrücken und neue daten hinten dran hängen

      Eggord- - Sonstige Problemstellungen

    • Array wird überschrieben

      LukiLeu- - Sonstige Problemstellungen

    • Array als Rückgabe einer Funktion

      brichun- - Sonstige Problemstellungen

  • 5 Benutzer haben hier geschrieben

    • WhitePage (5)
    • Gast (3)
    • fichz (2)
    • ErfinderDesRades (2)
    • RodFromGermany (1)
Array-Werte an anderes Array übergeben, nicht nur Referenz - VB-Paradise 2.0 – Die große Visual-Basic- und .NET-Community (2024)

FAQs

What is an array of arrays in VB? ›

An array is a consecutive group of memory locations that all have the same name and the same type. To refer to a particular location or element in the array, we specify the array name and the array element position number. The Individual elements of an array are identified using an index.

How to store an array in Visual Basic? ›

You can store and retrieve values in an array by referencing each array location by using its index enclosed in parentheses. Indexes for multidimensional arrays are separated by commas (,). You need one index for each array dimension. The following example shows some statements that store and retrieve values in arrays.

What are the 3 examples of array? ›

They can be used in various applications, including sorting and searching algorithms, database indexing, or even graphics processing. There are different types of arrays, including linear arrays, multidimensional arrays, and jagged arrays.

What is an array and explain it? ›

Overview. An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. Depending on the language, array types may overlap (or be identified with) other data types that describe aggregates of values, such as lists and strings.

Can you store data in array? ›

An array is a linear data structure that collects elements of the same data type and stores them in contiguous and adjacent memory locations. Arrays work on an index system starting from 0 to (n-1), where n is the size of the array.

What is array size? ›

The Length of an array in C refers to the number of elements in the array. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all of its elements.

How do you store an array list? ›

An array can be converted to an ArrayList using the following methods:
  1. Using the ArrayList. add() method to Manually add the Array elements in the ArrayList:
  2. Using Arrays. asList() method of java. ...
  3. Using Collections. addAll() method of java. ...
  4. Using Arrays. ...
  5. Using List.of(Elements) method of java.utils.List Interface:
Mar 8, 2024

Which is an array of arrays? ›

An array of arrays, also known as a multi dimensional array :-) A Matrix is only one of the structures that can be represented by such an array, when all first level elements are of the same size.

What is array of arrays also called *? ›

A multidimensional array is basically an array of arrays.

What do you call an array inside an array? ›

An array is an ordered collection of values: each value is called an element, and each element has a numeric position in the array, known as its index. JavaScript lets us create arrays inside array called Nested Arrays.

What is the data structure of array of arrays? ›

A multi-dimensional array is essentially an array of arrays, extending beyond the concept of a one-dimensional array. While a one-dimensional array is a linear collection of elements, a multi-dimensional array can be visualized as a matrix or a grid, where data is arranged in rows and columns.

Top Articles
Résultats De Recherche Pour 'Jeu Gold Miner Vegas Jeux Clic' - VitalityGames.com
Aaj Ka kanya Rashifal - कन्या राशिफल, kanya rashifal in Hindi, kanya Rashi today Rashifal
Benchmark Physical Therapy Jobs
Citi Trends Watches
Used Trucks for Sale in Oneida, TN (with Photos)
D&C Newspaper Obituaries
Lovex Load Data | xxlreloading.com
Pogo Express Recharge
Umass Medhub
Rick Lee Oaklawn Park Picks Today
Craigslist Richmond Va
Spacebar Counter - Space Bar Clicker Test
Pokemon Infinite Fusion Good Rod
Tyson Employee Paperless
U-Haul Customer Service & Support
Cookie Clicker The Advanced Method
Unterschied zwischen ebay und ebay Kleinanzeigen: Tipps, Vor- und Nachteile
1V1.Lol Pizza Edition
8 Casablanca Restaurants You’ll Want to Fly For | Will Fly for Food
Lexi Ainsworth Baby
Somewhere In Queens Showtimes Near The Maple Theater
Carefirst.webpay.md
12 Week Glute Program to Transform Your Booty with Free PDF - The Fitness Phantom
How Much Is Felipe Valls Worth
Moss Adams Client Portal
Irish DNA | Irish Origenes: Use your DNA to rediscover your Irish origin
Nyu Paralegal Program
Pella Culver's Flavor Of The Day
Sdn Upstate 2023
2011 Traverse Belt Diagram
Apple iPhone SE 2nd Gen (2020) 128GB 4G (Very Good- Pre-Owned)
Proctor Funeral Home Obituaries Beaumont Texas
Cornerstone Okta T Mobile
Ucla Course Schedule
Deborah Clearbranch Psychologist Georgia
It Might Get Smoked Nyt
Scarabaeidae), with a key to related species – Revista Mexicana de Biodiversidad
Nahant Magic Seaweed
Lagniappemobile
Latest News & Breaking News Coverage | Flipboard
600 Aviator Court Vandalia Oh 45377
Appsanywhere Mst
Ces 2023 Badge Pickup
German American Bank Owenton Ky
Black Panther Pitbull Puppy For Sale
Kingdom Tattoo Ithaca Mi
Loredana Chivu, despre operațiile făcute la clinica anchetată: "Am fost la un pas de moarte"
When His Eyes Opened Chapter 191
Shaver Lake Webcam Gas Station
Santa Rosa Craigslist Free Stuff
Ericdoa Ethnicity
Latest Posts
Article information

Author: Jerrold Considine

Last Updated:

Views: 6018

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Jerrold Considine

Birthday: 1993-11-03

Address: Suite 447 3463 Marybelle Circles, New Marlin, AL 20765

Phone: +5816749283868

Job: Sales Executive

Hobby: Air sports, Sand art, Electronics, LARPing, Baseball, Book restoration, Puzzles

Introduction: My name is Jerrold Considine, I am a combative, cheerful, encouraging, happy, enthusiastic, funny, kind person who loves writing and wants to share my knowledge and understanding with you.