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....??
6 1452
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
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...?
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.
Then you should use a loop to through the ArrayList.
Loops are soooo Pascalian ;-) Recursion is our friend here: -
private void update(List list, Object old, Object subst) {
-
int index= list.indexOf(old);
-
if (index >= 0) {
-
list.set(index, subst);
-
update(list.sublist(index+1, list.length());
-
}
-
}
-
kind regards,
Jos ;-)
Loops are soooo Pascalian ;-) Recursion is our friend here: -
private void update(List list, Object old, Object subst) {
-
int index= list.indexOf(old);
-
if (index >= 0) {
-
list.set(index, subst);
-
update(list.sublist(index+1, list.length());
-
}
-
}
-
kind regards,
Jos ;-)
Ah, so neat.
And it does cater for the case when old and subst are the same.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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?
...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |