954,198 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to declare array of struct

how to write a code for struct array

shadowrider
Newbie Poster
20 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 
how to write a code for struct array

google?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Would be along these lines:

public struct MyStruct

      {

        public string text;

        public string text2;

        public int number;

      }


http://www.codeproject.com/useritems/InterOp.asp
http://www.java2s.com/Code/CSharp/Collections-Data-Structure/Array.htm

Some basic information

RwCC
Junior Poster
177 posts since Jan 2006
Reputation Points: 70
Solved Threads: 4
 

Hey Shadowwrider,


ArrayList ar = new ArrayList();
object[] myarray = new object[2];


ArrayList comes from the System.Collection name space. I find it very useful, and will show you how to use the array or arraylist. Above I declare both for your consideration. Notice that for the standard array, you need to declare it as an array of objects.

In this example we have a very simple struct:

public struct MyStruct
{
public int t1;
public string s1;
}


Here is a Simple example using some Console writing:

ArrayList ar = new ArrayList();
object[] myarray = new object[2];
// First get an instance of the struct
MyStruct thisjoe;
thisjoe.t1 = 1;
thisjoe.s1 = "Hello";
myarray[0] = thisjoe; // Add to the Array
ar.Add(thisjoe); // OR add to the ArrayList

// Do another one
thisjoe.t1 = 2;
thisjoe.s1 = "World";
ar.Add(thisjoe);
myarray[1] = thisjoe;

// How to get a value ? Lets put one into (x)
int x = ((MyStruct)ar[0]).t1;
Console.WriteLine("X={0}", x);

// Lets go through the ArrayList
foreach (MyStruct myjoe in ar)
{
Console.WriteLine("{0}:{1}", myjoe.s1, myjoe.t1);
}

// Lets go through the standard Array
foreach (MyStruct myjoe in myarray)
{
Console.WriteLine("{0}:{1}", myjoe.s1, myjoe.t1);
}


Hope this Helps,
Jerry

JerryShaw
Posting Pro in Training
465 posts since Nov 2006
Reputation Points: 69
Solved Threads: 75
 

package JavaProject2;
//import java.util.Scanner;
public class Student1
{ static int[][] arr = {
{ 1, 2, 3, },
{ 4, 5, 6, },
};


public static void main(String args[][])
{
for(int i=0;i < 3;i++)
{
for(int j=0;j<2;j++)
{
System.out.println(arr[i][j]);
}
}

}
}






/*
String y;
String[][] NR={};
Scanner raw=new Scanner(System.in);
String stu[]={"Abhishek","Ranjeet","Bibhakar","Pradeep","Rahul","Washim","Siraj","Prity","lovely","Suchitra"};
String var,var1;
int i,j,k=0;
public void Name (String var1)
{
for (i=0;ihttp://www.danwed.com

abhisek.verma
Newbie Poster
3 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

package JavaProject2; //import java.util.Scanner; public class Student1 { static int[][] arr = { { 1, 2, 3, }, { 4, 5, 6, }, };

public static void main(String args[][]) { for(int i=0;i < 3;i++) { for(int j=0;j<2;j++) { System.out.println(arr[i][j]); } }

} } /* String y; String[][] NR={}; Scanner raw=new Scanner(System.in); String stu[]={"Abhishek","Ranjeet","Bibhakar","Pradeep","Rahul","Washim","Siraj","Prity","lovely","Suchitra"}; String var,var1; int i,j,k=0; public void Name (String var1) { for (i=0;ihttp://www.danwed.com

This isn't java. Next time use code tags.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You