how to write a code for struct array

Member Avatar for iamthwee

how to write a code for struct array

google?

Hey Shadowwrider,

<code>
ArrayList ar = new ArrayList();
object[] myarray = new object[2];
</code>

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:
<code>
public struct MyStruct
{
public int t1;
public string s1;
}
</code>

Here is a Simple example using some Console writing:
<code>
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);
}
</code>

Hope this Helps,
Jerry

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[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;i<stu.length;i++)
var1=var1+var1+"\n";
}


public void fees()
{
for (i=0;i<stu.length;i++)
{    System.out.println(stu);
var=raw.next();
//System.out.println(var);
if (var.matches("y"))
{


NR[k]=stu+"   Paid"+"\n";
k++;
}
}
for (i=0;i<30;i++)
System.out.println("\n");
for (i=0;i<10;i++)
{
for (j=0;j<1;j++)
System.out.println(NR[k]=stu+"   Paid"+"\n");
}
//System.out.println(var1);
}
}


*/http://www.danwed.com
Member Avatar for iamthwee

This isn't java.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.