• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unicode for java  RSS feed

 
nida lulu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I've never tried to implement any Unicode in my coding. I am creating an app that stores CD names etc. I wanted to make my program more efficient, namely the searching for a CD and had the idea that if I had a 2D array so that CD titles that start with A are in one array and titles that start with B are in another array. What i was thinking was that i can have an offset to store the digit value of Unicode (ex: offset = 65 for A) and if a title starts with A to store it in that specific array with all other A starting titles. My problem is I don't understand how to use unicode well enough to say: If (title starts with A) {
match it to Unicode 65;
index = offset - 65; } So that the value is stored in index = 0 of the referencing array
hopefully you understand what I mean.
any help is appreciated, thnx
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The unicode values are in order, meaning that B is after A, C is after B, etc. So... given a character, char, the index could simply be, char - 'A'. This will give you zero for A, one for B, all the way to twenty-five for Z.

Henry
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

I don't think Unicode has much to do with what you're asking. For starters, I would ask: do you really need to do this? If you use a List to hold the CD titles, and keep the list sorted, then you can search for a title using Collections.binarySearch which is quite fast.

But since the data you have will include more than just the title, you'll probably end up using a Map instead - each title has an associated object that contains all the other CD data. Searching a Map is fast, too.

I'd look into these data structures before starting to optimize.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An implementation of the SortedMap interface maybe?
 
nida lulu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, i think i understand what you mean Henry. I am going to store an offset at the value 25 and use Character.getNumericValue(aChar);
so that if i find a char that happens to be A it will be offset-numericValue to give me an index of 0.
I am still kinda new to java and have only ever used C++ before, so I'm not sure what exactly a map is. Is it more along the lines of intermediate java?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getNumericValue doesn't return 65 from 'A'. You will need to look it up and see what it actually returns.

And a Map is one of the interfaces in the Collections Framework. You will find details from those links.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And remember that char primitives are themselves numbers, so you can do arithmetic on them.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!