Results 1 to 6 of 6
  1. #1
    Neokil is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default ArrayList.Find() Java Equivalent

    Hello Guys,

    I'm not that active in writing Java code but now i have to translate some of my Data-Classes to Java for some new Client Applications.
    I'm fine with most things for the Classes are almost completely based on the Standarttypes only (and many subclasses).
    But there are some things i can't find an equivalent:
    Java Code:
    ArrayList<CustomClass> TableColumns;
    
    return Data[TableColumns.IndexOf(TableColumns.Find(c => c.Name == col))];
    I could look for it using some foreach-loops, but maybe there is a different/easier solution to that?

    Another thing is, i need to have a reference to the Parent-Class, so i can access some Values to catch some errors.
    As far as I know there is no "pass by reference" available in Java, but maybe something to work around it?
    Java Code:
    public TableRow(Object[] Data, ref List<CustomClass> Parent)
    {
           //...
    }

    Greetings
    Neokil

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: ArrayList.Find() Java Equivalent

    Instead of using a find function, you could store your values in a map.

    Java is pass by value, but you should have access to the non-private values in a parent class. Can you give us an SSCCE showing what you're trying to do? Parent class could mean a couple things...
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Neokil is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: ArrayList.Find() Java Equivalent

    Ok, lets see...
    Java Code:
    public class Table
    {
        List<TableRow> tableData;
        List<TableColumn> tableColumns;
        
        public TableRow this[int r]
        {
            get
            {
                return tableData[r];
            }
        }
    }
    Java Code:
    public class TableColumn
    {
        public String Name { get; set; }
        public Type ColumnType { get; set; }
    }
    Java Code:
    public class TableRow
    {
        private List<TableColumn> TableColumns;
        private Object[] Data;
    
        public Object this[String col]
        {
           get
           {
               try
               {
                   return Data[TableColumns.IndexOf(TableColumns.Find(c => c.Name == col))];
               }
               catch (Exception ex)
               {
                   Console.WriteLine("ERROR: " + ex.Message);
                   return null;
               }
           }
        }
    The thing is, i need an up-to-date-Version of the TableColumns to get return the right value.
    (Column names can change during execution, as well as new columns can be added.)

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: ArrayList.Find() Java Equivalent

    The thing is, i need an up-to-date-Version of the TableColumns to get return the right value.
    ... and the problem is? This is just a statement, not a question. Be more specific. Where is the data coming from? How are you fetching it? WHEN are you fetching it?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Neokil is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: ArrayList.Find() Java Equivalent

    The Data can come from several places. Its a class of a general Libary we use for nearly all of our programs.
    Most times the general procedure is:
    - Override the Output-Method
    - Fill the Table with data from the SQL-Server
    - Add some Columns for calculation
    - Rename the Columns as you want them to be in the Output-File

    In the Programm you get the Data of a specific field by using the Array-Operator Table[RowIndex][ColumnIndex].
    For better readability and easier troubleshooting the Column should be accessed throught the Columnname, so we can use Table[RowIndex]["ColumnName"]
    Another thing about accessing the column by name is that we may need to add another column to the database so the index could change, but not the name.
    To find out which Index belongs to the specific Name i need to access the TableColumns and in C# I solved this by passing a reference to the TableColumns and now I need to know how to bypass the "pass by value" in Java to have the same result as I have in C#.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: ArrayList.Find() Java Equivalent

    That's your problem: you have C# code and now you are trying to make Java do exactly the same. But Java is not exactly the same as C#, so you are not going to be able to do that.

    In stead forget about the code and restart from a functional perspective; C# does not exist. You want to achieve something. How do you achieve that functionality through Java? I think you'll find that researching how to implement your specifications will be far more productive than trying to figure out how to convert code.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. java (7) equivalent of .Net timespan
    By .paul. in forum New To Java
    Replies: 12
    Last Post: 07-29-2012, 05:38 PM
  2. ArrayList find values
    By ralf in forum New To Java
    Replies: 39
    Last Post: 07-07-2011, 12:08 AM
  3. Replies: 1
    Last Post: 04-14-2011, 02:48 PM
  4. Equivalent of PHP isset in JAVA
    By africanhacker in forum New To Java
    Replies: 11
    Last Post: 03-24-2011, 03:41 PM
  5. Java equivalent of Linux 'find' function
    By porchrat in forum New To Java
    Replies: 5
    Last Post: 08-25-2010, 12:31 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
  •