Results 1 to 11 of 11
  1. #1
    Jackblue is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default Indexing java arrays with string

    Hello everyone,

    I'd like to have string indices for an mxn array in java (a matrix). I mean, instead of numbers I'd like to have strings as indices.

    For example:
    By assingment instead of having a[1][3] = 10; I would have a["x"]["y"] = 10;

    The idea I have is to use another data structure such as a set or a hashtable. But I'd like to keep using arrays for performance reasons on the other hand I do need string indices.

    The other idea is to have an array for the values and two indexing Vectors. One for the rows and the other for the columns. But before preoceeding with this solution, I'd like to ask if some one has a better solution.

    Thanks in advance

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Sorry, can't do it, directly, and the two "Vectors" would probably wind up being slower than using a Map directly.

    Java Code:
    Map<String, Map<String, Integer>> test = new HashMap<String, Map<String, Integer>>();
    
    Map<String, Integer> inner = new HashMap<String, Integer>();
    inner.put("A", 5);
    inner.put("B", 5);
    test.put("A", inner);
    
    inner = new HashMap<String, Integer>();
    inner.put("A", 6);
    inner.put("B", 6);
    test.put("B", inner);
    
    System.out.println(test.get("A").get("B"));

  3. #3
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    What you are describing is an associative array (allowing string values for the index), and this is implemented in Java as the Map class. This would be my suggestion.


    Java Code:
    import java.util.HashMap;
    
    public class Matrix<V> extends HashMap<String, V>
    {
    	Matrix()
    	{
    		super();
    	}
    	
    	V put(String row, String col, V value)
    	{
    		return put(wrap(row, col), value);
    	}
    	
    	V get(String row, String col)
    	{
    		return get(wrap(row, col));
    	}
    	
    	/**
    	 * Returns the key that represents the specified [row][col]
    	 */
    	private String wrap(String row, String col)
    	{
    		return "[" + row + "][" + col + "]";
    	}
    	
    	/**
    	 * Main function
    	 * 
    	 * @param args
    	 *            (not used)
    	 */
    	public static void main(String[] args)
    	{
    		Matrix<Integer> a = new Matrix<Integer>();
    		
    		a.put("x", "y", 10);
    		
    		System.out.println(a.get("x", "y"));
    	}
    }
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    It would be nice to know, however, exactly what the "Strings" are, and what they have to do with the matrix, as there may be a much better solution.

  5. #5
    Jackblue is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    Thank you for your replies.
    The matrix I have is a "term-document" matrix, i.e., the matrix used as input for an information retrieval algorithm like "latent semantic analysis". The matrix will undergo a set of matrix opperation like sigular value decompostion, multiplication etc. So it realy makes sense to have an efficient data structure. At the same time I need always to know which value correspond to which term and document.

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    If you're going to do matrix math, why use string indices?? Can you elaborate on why you want the string indices for, again?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Then "indexing" ArrayLists (not Vectors if the lists themselves do not necessarily need to be thread-safe, and if they do but will only written once, then use ArrayList and Collections.unmmodifiableList, they will be much quicker than a Vector) is probably a better idea, since I am assuming you won't be neding the "names" for the actual math, but rather, only for retreiving certains values at certain times, or maybe only for reassociating the finished matrix with the original data.

  8. #8
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Congrats on post 1000.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  9. #9
    Jackblue is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    May be I wrongly stated what I need. Anyway the following example may clarify that:

    Given the following matrix(A):
    a b
    c 1 3
    d 2 1
    e 0 5

    A[0][1] = 3 and that means the A[c][b] = 3,
    A[1] = {2,1} it means the values of the row d are (2 and 1).
    So I need to know which value(s) corresponde(s) to which row, column or row-column combination (of course, according to the string lables of the columns and rows).

  10. #10
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    OK, the revised Matrix class.

    Java Code:
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
    
    public class Matrix
    {
    	int[][] matrix;
    
    	/**
    	 * Mapping from String to row number
    	 */
    	Map<String, Integer> rowNums = new HashMap<String, Integer>();
    
    	/**
    	 * Mapping from String to column number
    	 */
    	Map<String, Integer> colNums = new HashMap<String, Integer>();
    
    	Matrix(int rows, int cols)
    	{
    		matrix = new int[rows][cols];
    	}
    
    	Matrix(int[][] matrix)
    	{
    		this.matrix = matrix;
    	}
    
    	int get(String row, int col)
    	{
    		return get(rowNums.get(row), col);
    	}
    
    	int get(int row, String col)
    	{
    		return get(row, colNums.get(col));
    	}
    
    	int get(String row, String col)
    	{
    		return get(rowNums.get(row), colNums.get(col));
    	}
    
    	int get(int row, int col)
    	{
    		return matrix[row][col];
    	}
    
    	int set(String row, int col, int value)
    	{
    		return set(rowNums.get(row), col, value);
    	}
    
    	int set(int row, String col, int value)
    	{
    		return set(row, colNums.get(col), value);
    	}
    
    	int set(String row, String col, int value)
    	{
    		return set(rowNums.get(row), colNums.get(col), value);
    	}
    
    	int set(int row, int col, int value)
    	{
    		int previousValue = matrix[row][col];
    
    		matrix[row][col] = value;
    
    		return previousValue;
    	}
    
    	Integer assignRow(String row, Integer rowNum)
    	{
    		return rowNums.put(row, rowNum);
    	}
    
    	Integer assignCol(String col, Integer colNum)
    	{
    		return colNums.put(col, colNum);
    	}
    
    	void assignRows(String... rows)
    	{
    		for (int i = 0; i < rows.length; i++) {
    			assignRow(rows[i], i);
    		}
    	}
    
    	void assignCols(String... cols)
    	{
    		for (int i = 0; i < cols.length; i++) {
    			assignCol(cols[i], i);
    		}
    	}
    
    	int[] row(String row)
    	{
    		return row(rowNums.get(row));
    	}
    
    	int[] row(int row)
    	{
    		return matrix[row];
    	}
    
    	int rows()
    	{
    		return matrix.length;
    	}
    
    	int cols()
    	{
    		return matrix[0].length;
    	}
    
    	int[][] col(String col)
    	{
    		return col(colNums.get(col));
    	}
    
    	int[][] col(int col)
    	{
    		int rows = rows();
    		int[][] column = new int[rows][1];
    
    		for (int i = 0; i < rows; i++) {
    			column[i][1] = matrix[i][col];
    		}
    
    		return column;
    	}
    
    	/**
    	 * Main function
    	 * 
    	 * @param args
    	 *            (not used)
    	 */
    	public static void main(String[] args)
    	{
    		int[][] a = { { 1, 3 }, { 2, 1 }, { 0, 5 } };
    
    		Matrix matA = new Matrix(a);
    		matA.assignCols("a", "b");
    		matA.assignRows("c", "d", "e");
    
    		// outputs 3
    		System.out.println(matA.get("c", "b"));
    
    		// outputs [2, 1]
    		System.out.println(Arrays.toString(matA.row("d")));
    	}
    }
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  11. #11
    Jackblue is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Problems after re-indexing SQL server
    By ahartley in forum Lucene
    Replies: 1
    Last Post: 11-04-2009, 06:07 PM
  2. file indexing
    By vislawath.jawaharlal in forum Threads and Synchronization
    Replies: 0
    Last Post: 04-07-2009, 01:15 PM
  3. [SOLVED] indexing an element in an array help!
    By anthonym2121 in forum New To Java
    Replies: 1
    Last Post: 04-03-2009, 06:21 PM
  4. Lucene Re-Indexing
    By connect2srinath in forum Lucene
    Replies: 1
    Last Post: 05-11-2008, 05:35 PM
  5. Indexing starting with 1
    By ravian in forum New To Java
    Replies: 4
    Last Post: 01-04-2008, 12:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •