how do i access data structure in different class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: how do i access data structure in different class

  1. #1
    Join Date
    Apr 2003
    Posts
    1

    how do i access data structure in different class

    Hi , Please help me, i've created a datastructure in class Pos, which is used in class ass1.

    the Pos type is changed to ass1 type in ass1 class
    with the function ass1()

    so then i have a "ass1 array[][]"
    now, i am having problem accessing the Pos individual elements in this array ..
    can't really do system.out.print(array[1][1].value);

    how do i access them ?






    /////////////////////////////CODE/////////////////////////
    import java.util.*;
    import java.io.*;
    import java.text.*;
    import java.lang.*;
    import java.util.*;

    class Pos{

    int value, i;
    int S, W, E, N;

    Pos(int newValue, int s, int w, int e, int n){
    value = newValue;
    S = s; W = w; E = e; N = n;
    }
    }

    public class ass1{

    private Pos root;
    static Pos p;
    static int [][] circuit_board = new int[11][11];
    static Pos [][] circuit_board1 = new Pos[11][11];
    static ass1 [][] circuit_board2 = new ass1[11][11];
    static String line, cell;
    static StringTokenizer tokenizer;

    public ass1(){root = null;}

    public static Pos insert(Pos t, int newValue, int s, int w, int e, int n){
    t = new Pos(newValue, s, w, e, n);
    return t;
    }

    public void insert(int newValue, int s, int w, int e, int n){
    root = insert(root, newValue, s, w, e, n);
    }

    public void add(ass1 t, int newValue, int s, int w, int e, int n){
    insert(newValue, s, w, e, n);
    }

    public static void printBoard(int [][] board){

    for(int i=0;board[i][0] != -1; i++){
    for(int j=0;board[0][j] != -1;j++){
    System.out.print(board[i][j]);
    }
    System.out.print("\n");
    }
    }

    public static int findXsize(int array[][]){
    int i;
    for(i=0;array[0][i] != -1;i++);
    return i-1;
    }

    public static int findYsize(int array[][]){
    int i;
    for(i=0;array[i][0] != -1;i++);
    return i-1;
    }

    public static ass1[][] array2D2Ass1Array(int array[][]){

    int i, j;
    ass1 t = new ass1();
    for(i=0; array[i][0]!= -1 ;i++){
    for(j=0; array[i][j] != -1; j++){
    if(i == 0 && j == 0){t.add(t, array[i][j], array[i+1][j], -1,array[i][j+1], -1);}
    else if(i == 0){t.add(t, array[i][j], array[i+1][j], array[i][j-1], array[i][j+1], -1);}
    else if(j == 0){t.add(t, array[i][j], array[i+1][j], -1, array[i][j+1], array[i-1][j]);}
    else{ t.add(t, array[i][j], array[i+1][j], array[i][j-1], array[i][j+1], array[i-1][j]);}

    circuit_board2[i][j] = t;
    }
    }
    return circuit_board2;
    }

    public static int[][] input2Array2D(String filename){
    int i=0, j=0, num=0;

    ass1 t = new ass1();

    try{
    FileReader fr = new FileReader("test.txt");
    BufferedReader inFile = new BufferedReader (fr);

    while((line = inFile.readLine()) != null){ //Get input
    tokenizer = new StringTokenizer(line);
    while(tokenizer.hasMoreTokens()){ //Check if more token left
    cell = tokenizer.nextToken();
    num = Integer.parseInt(cell); //Convert type string to int
    circuit_board[i][j] = num; //Initialise circiut board

    j++;
    }
    circuit_board[i][j] = -1;
    i++;
    j=0;
    }
    circuit_board[i][j] = -1; //Set termination point
    }

    catch(IOException exception){
    System.out.println(exception);
    }
    return circuit_board;
    }

    public static void main(String args[]){

    circuit_board2 = array2D2Ass1Array(input2Array2D("test.txt"));

    }
    }

    ///////////////////////////////END OF CODE////////////////////

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    Firstly, I suggest you choose new names for your classes - I got utterly confused when reading your instructions. Also, it is the Java convention to capitalise the first character of a class name.

    Secondly, you access the elements of an array thus:
    Code:
      ...
      int [][] circuit_board = new int[11][11];
      System.out.println(circuit_board[1][2]);
      ...
    ArchAngel.
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center