Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RonnieMoir · Apr 20, 2015 at 03:50 AM · javascriptarrayarraysstring

Array of Array

I have been looking around for a while now but have found nothing which has fixed problem. So i'm asking you. I want to be able to store an Array of strings into an array. I have no idea how to go about this so if you can help I would be extremely grateful.

PS. I'm using unity script

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Mapleman · Apr 20, 2015 at 05:17 AM

You can't create an array of string arrays with .NET. At first, this might sound like nonsense, but all elements in array must be of the same type. Array of strings is clearly different type than just plain string. Put I assume that you want to create multidimensional string array, or perhaps a list of string arrays. Check code below:

     public class StringTester
     {
         private string[,] TwoDimensionalArray;
         private string[][] JaggedArray;
 
         private string[] OneDimensionalArray;
         private List<string[]> ListOfArrays;
 
         public void Test()
         {
 
             //Two dimensional array of strings
             TwoDimensionalArray = new string[10,10];
             TwoDimensionalArray[2, 2] = "Hello World";
 
             //Two dimensional jagged array (variable length arrays)
             JaggedArray = new string[5][];
             JaggedArray[0] = new string[2];
             JaggedArray[1] = new string[6];
             JaggedArray[0][0] = "Foo";
             JaggedArray[1][1] = "Bar";
 
             //List of string arrays
             ListOfArrays = new List<string[]>();
             ListOfArrays.Add(new string[10]);
             ListOfArrays.Add(new string[8]);
             ListOfArrays[0][0] = "Some text";
         }
     }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pangamini · Apr 20, 2015 at 08:16 AM 0
Share

What you said is not true, array of strings is a type just like any other. Indeed you can have an array of arrays of strings. That makes every element in the array to be of same type (array of strings). Anyway it's not true that all elements in the array have to be of same type. This is true for value types which are final and cannot be derived from. For any other classes you can indeed have an array that contains objects of different types that all derive from the same specified class.

avatar image Mapleman · Apr 20, 2015 at 04:16 PM 0
Share

Let's start with some basic things about arrays. Arrays are in fact blocks of memory reserved from the heap. When we want to access some element i from array, compiler actually addresses it like [Array_Start_address_in_heap]+[Element_size]*i.

In case of string arrays, elements in array are actually references to some other memory addresses in heap. And same goes to all other reference types. Arrays never hold the object as they are, only references to them. In case of value types, the values are directly stored in same continuos block of memory.

From these comes the fact the so called array of string of arrays just isn't possible. It would mean a structure like 'Array of refenrences to arrays of strings'. It becomes even more clear why this kind of construct isn't supported when you think if our array elements would be of some custom struct type (structs are value-types). So that would mean that we should have a construct where we would need to have array of references (4 or 8 bytes per element) where each elements points to an array of structs where each element can be any arbitrary size. So clearly the memory addressing wouldn't work.

But this was more of academic interest only. For the one who asked the original question, I think most flexible way would be using list of string arrays, or perhaps even list of list of strings.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make certain elements in an array rarer when using random selection? 4 Answers

Referencing private string for array name 2 Answers

[SOLVED] First array slot blocking second array slot 1 Answer

C# ArrayList match to string? 1 Answer

Converting a string to an int 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges