Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Large array's to Strings in Java

  • 07-02-2007 02:37PM
    #1
    Registered Users, Registered Users 2 Posts: 353 ✭✭


    Let me start by saying that I feel particularly dumb today so bear with me.

    My problem is to be the volume of data I'm working with.
    My data is contained in a text file arranged in 1000 rows of 2700 numbers.
    I was advised to convert this into an array to make it easier to access and doing that is trivial.
    The difficulty arises when I try to display the entire data in a TextArea. While creating the array is simple and quick, converting it into a single String is excrutiatingly slow. Unfortunatly I need to be able to display and operate on the data.
    Can any of you suggest either an alternative approach or any hints and tips to improve the execution?

    Thanks.


Comments

  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    Bunging 2,700 x 1,000 items into a huge string and then displaying that in a TextArea is indeed inefficient.

    Have you considered possibly using a TableModel and JTable to display it. This should be much better because you can implement the tablemodel interface and use this new object to wrap your large array.

    Implementing tablemodel for you will just mainly involve overriding getObject(row,col)
    getRowCount()
    getColumnCount()

    (plus a few other methods like addListener() ),

    but all the other methods you can just ignore as you are just displaying it. If you want to change the array behind the scenes and have the UI update you will need to understand and implement some sort of fireEvent code.

    Anyway it's quite simple, should be around 50 lines of code all told.


  • Registered Users, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭lynchie


    Also if your doing loads of String concatenations i.e. s+=s1+s2+s3 your better off using a StringBuffer instead.


  • Closed Accounts Posts: 362 ✭✭information


    lynchie wrote:
    Also if your doing loads of String concatenations i.e. s+=s1+s2+s3 your better off using a StringBuffer instead.
    I'll see your StringBuffer and raise you StringBuilder.

    Assuming the data from the text file will not change, you can:

    a)
    You read the textfile into a string first.
    Then split the string into the array.
    You can use the original string in the textarea

    or

    b)
    Using your current operation and then
    Just reread the data fromthe file into a string, for displaying in the textarea.


Advertisement