Two-Dimensional Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lucent2418
    New Member
    • Jul 2007
    • 3

    #1

    Two-Dimensional Array

    I am trying to return a two-dimensional array with data but am having some problems retreving the data. Can someone guide me what I should change in this code?

    Thanks

    Code:
    public ArrayList getData()
    {
    	ArrayList data = new ArrayList();
    	ArrayList myList1 = new ArrayList();
    	ArrayList myList2 = new ArrayList();
    	int i;
            
    	for(i=0; i<5; i++)
    	{
    		i++;
    		myList1.Add("San");
    	}
    
    	for(i=0; i<5; i++)
    	{
    		i++;
    		myList2.Add("Diego");
    	}
    
    	data.Add(myList1);
    	data.Add(myList2);
            
    return data;
    method for retreving the data

    Code:
    ArrayList Data = new ArrayList();
    DatabaseAccessLayer DAL = new DatabaseAccessLayer();
    data = DAL.getData;
    
    for(int i=0; i<data.Count; i++)
    {
    	Data = ((ArrayList)(data[i]));
    	Console.Write((string)(Data[0]));
    	Console.Write((string)(Data[1]));
    }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You are itterating your counter (i++) two times in each loop in your populating thing.

    Otherwise I don't see why it wouldn't work

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by Lucent2418
      I am trying to return a two-dimensional array with data but am having some problems retreving the data. Can someone guide me what I should change in this code?

      Thanks

      Code:
      public ArrayList getData()
      {
      	ArrayList data = new ArrayList();
      	ArrayList myList1 = new ArrayList();
      	ArrayList myList2 = new ArrayList();
      	int i;
              
      	for(i=0; i<5; i++)
      	{
      		i++;
      		myList1.Add("San");
      	}
      
      	for(i=0; i<5; i++)
      	{
      		i++;
      		myList2.Add("Diego");
      	}
      
      	data.Add(myList1);
      	data.Add(myList2);
              
      return data;
      method for retreving the data

      Code:
      ArrayList Data = new ArrayList();
      DatabaseAccessLayer DAL = new DatabaseAccessLayer();
      data = DAL.getData;
      
      for(int i=0; i<data.Count; i++)
      {
      	Data = ((ArrayList)(data[i]));
      	Console.Write((string)(Data[0]));
      	Console.Write((string)(Data[1]));
      }
      Note that a two-dim array is not the same as an arraylist of arraylists.
      You should also consider using a typed collection.

      Comment

      • Lucent2418
        New Member
        • Jul 2007
        • 3

        #4
        Thanks. I've rearranged the code and it now returns data. This is an arraylist made up of several arraylists.

        How could I organize the data to return in this manner:

        A1x
        B2x
        C3x
        and so on.

        Code:
        ArrayList data;
        ArrayList Data = new ArrayList();
        DatabaseAccessLayer DAL = new DatabaseAccessLayer();
        data = DAL.getData();
         
        for(int i=0; i<data.Count; i++)
        {
        	Data = ((ArrayList)(data[i]));
        	Console.WriteLine((string)(Data[0]));
        	Console.WriteLine((string)(Data[1]));
        	Console.WriteLine((string)(Data[2]));
        	Console.WriteLine((string)(Data[3]));
        	Console.WriteLine((string)(Data[4]));
        	Console.WriteLine((string)(Data[5]));
        	Console.WriteLine((string)(Data[6]));
        	Console.WriteLine((string)(Data[7]));
        }
        Sample data

        Code:
        public ArrayList getData()
        {
        	ArrayList data = new ArrayList();
        
        	ArrayList myList1 = new ArrayList();
        	ArrayList myList2 = new ArrayList();
        	ArrayList myList3 = new ArrayList();
                
        	myList1.Add("A");
        	myList1.Add("B");
        	myList1.Add("C");
        	myList1.Add("D");
        	myList1.Add("E");
        	myList1.Add("F");
        	myList1.Add("G");
        	myList1.Add("H");
        
        	myList2.Add("1");
        	myList2.Add("2");
        	myList2.Add("3");
        	myList2.Add("4");
        	myList2.Add("5");
        	myList2.Add("6");
        	myList2.Add("7");
        	myList2.Add("8");
         
        	myList3.Add("x");
        	myList3.Add("x");
        	myList3.Add("x");
        	myList3.Add("x");
        	myList3.Add("x");
        	myList3.Add("x");
        	myList3.Add("x");
        	myList3.Add("x");
        
        	data.Add(myList1);
        	data.Add(myList2);
        	data.Add(myList3);
                
        	return data;
        }

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          [CODE=css]for(int j = 0; j < ((ArrayList)lis t[0]).Count; j ++) {
          String val = "";
          for(int i =0; i < list.Count;i++) {
          val += (String)((Array List)list[i])[j];

          }
          Console.WriteLi ne(val);

          } [/CODE]

          Generic list doesn't require the ugly casts

          Comment

          Working...