473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Array List in collections

11
I hav a doubt regarding the array lists......
check out the program below....

List l=new ArrayList();
l.add(0,"hell");
l.add(1,"hello");
l.add(2,"hello");
l.add(3,"hello");
l.add(4,"hello");
System.out.println("Index: "+(l.indexOf("hello")));
System.out.println("Index: "+(l.indexOf("hello")));

OUTPUT:index:1
index:1

my doubt is.....evrytime I get the index of the 1st object......what if I want 2 get the index of the duplicate objects other than the 1st and the last objects....??
Oct 15 '07 #1
6 1452
JosAH
11,448 Expert 8TB
I hav a doubt regarding the array lists......
check out the program below....

List l=new ArrayList();
l.add(0,"hell");
l.add(1,"hello");
l.add(2,"hello");
l.add(3,"hello");
l.add(4,"hello");
System.out.println("Index: "+(l.indexOf("hello")));
System.out.println("Index: "+(l.indexOf("hello")));

OUTPUT:index:1
index:1

my doubt is.....evrytime I get the index of the 1st object......what if I want 2 get the index of the duplicate objects other than the 1st and the last objects....??
Yep, unlike the String class Lists don't have two parameter indefOf() method; a
bit inconsistent, but once you've found a first index, you can update an 'offset'
and find the other elements on a sublist of that List and iterate on it again.
There also is a lastIndexOf() method if you need it.

kind regards,

Jos
Oct 15 '07 #2
Ale
11
Yep, unlike the String class Lists don't have two parameter indefOf() method; a
bit inconsistent, but once you've found a first index, you can update an 'offset'
and find the other elements on a sublist of that List and iterate on it again.
There also is a lastIndexOf() method if you need it.

kind regards,

Jos
what if i dot want 2 update the 1st one....but all the other duplicate objects...?
Oct 16 '07 #3
r035198x
13,262 8TB
what if i dot want 2 update the 1st one....but all the other duplicate objects...?
Then you should use a loop to through the ArrayList.
Oct 16 '07 #4
JosAH
11,448 Expert 8TB
Then you should use a loop to through the ArrayList.
Loops are soooo Pascalian ;-) Recursion is our friend here:

Expand|Select|Wrap|Line Numbers
  1. private void update(List list, Object old, Object subst) {
  2.    int index= list.indexOf(old);
  3.    if (index >= 0) {
  4.       list.set(index, subst);
  5.       update(list.sublist(index+1, list.length());
  6.    }
  7. }
  8.  
kind regards,

Jos ;-)
Oct 16 '07 #5
r035198x
13,262 8TB
Loops are soooo Pascalian ;-) Recursion is our friend here:

Expand|Select|Wrap|Line Numbers
  1. private void update(List list, Object old, Object subst) {
  2.    int index= list.indexOf(old);
  3.    if (index >= 0) {
  4.       list.set(index, subst);
  5.       update(list.sublist(index+1, list.length());
  6.    }
  7. }
  8.  
kind regards,

Jos ;-)
Ah, so neat.
And it does cater for the case when old and subst are the same.
Oct 16 '07 #6
JosAH
11,448 Expert 8TB
Ah, so neat.
And it does cater for the case when old and subst are the same.
Sure, and a smart compiler can even remove the tail recursion; if it does it
effectively turns the recursive version into an iterative version automagically ;-)

kind regards,

Jos
Oct 16 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
1
by: typeMisMatch | last post by:
Hi, This may sound simple, here is my problem. I have a couple classes, Class1, Class2, Class3. Class1 has a collection of Class2 and Class3. Right now those collections are defined as...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
5
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
3
by: John Devlon | last post by:
Hi Can anyony please tell me why I should use a List of objects instead of an array of objects ? Thanx John
10
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! Got a simple question. I am new to c# but this is not making me any sence. If i declare: string myStringArray = new string; How the heck could i fill it with more than one element? ...
4
by: Sharon | last post by:
hello, I wanted to populate an array with the data from sql table, but not sure how to go about it. This is the array iam using at present, but i dont want to provide the values. Instead i...
2
by: Big Charles | last post by:
Hello, I would like to create an array-class to be able to call like: Dim oMyCar as New MyCar ' After initializing oMyCar, the object has to be like: oMyCar(0).Brand...
6
by: Glenn | last post by:
OK, need help in translating what I'd like to do in old school C to best method C# If I was wanting to create an array of struct's, writing and reading them to/from a file.. how would I do this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.