473,378 Members | 1,446 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,378 software developers and data experts.

Array Lists

Hello All,
Thank you in advance. I have a quick question about array lists. Why
does the following code give me the same results if I reference two
different elements?

Dim myList As New ArrayList
Dim test(9) As String
test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"
test(4) = "E"
test(5) = "F"
test(6) = "G"
test(7) = "H"
test(8) = "I"
test(9) = "J"
myList.Add(test)

test(0) = "1"
test(1) = "2"
test(2) = "3"
test(3) = "4"
test(4) = "5"
test(5) = "6"
test(6) = "7"
test(7) = "8"
test(8) = "9"
test(9) = "10"
myList.Add(test)

TextBox1.Text = myList(0)(4) '= 5 (Incorrect. Should = E)
TextBox1.Text = myList(1)(4) '= 5 (Correct)

Could you please explain what is happening?

Thank you

Nov 29 '05 #1
5 1062
Dim test(9) As String
test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"
test(4) = "E"
test(5) = "F"
test(6) = "G"
test(7) = "H"
test(8) = "I"
test(9) = "J"
myList.Add(test)
Dim test1(9) As String

test1(0) = "1"
test1(1) = "2"
test1(2) = "3"
test1(3) = "4"
test1(4) = "5"
test1(5) = "6"
test1(6) = "7"
test1(7) = "8"
test1(8) = "9"
test1(9) = "10"
myList.Add(test1)

TextBox1.Text = myList(0)(4) '= 5 (Incorrect. Should = E)
TextBox2.Text = myList(1)(4) '= 5 (Correct)

"unwantedspam" <un**********@mchsi.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hello All,
Thank you in advance. I have a quick question about array lists. Why
does the following code give me the same results if I reference two
different elements?

Dim myList As New ArrayList
Dim test(9) As String
test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"
test(4) = "E"
test(5) = "F"
test(6) = "G"
test(7) = "H"
test(8) = "I"
test(9) = "J"
myList.Add(test)

test(0) = "1"
test(1) = "2"
test(2) = "3"
test(3) = "4"
test(4) = "5"
test(5) = "6"
test(6) = "7"
test(7) = "8"
test(8) = "9"
test(9) = "10"
myList.Add(test)

TextBox1.Text = myList(0)(4) '= 5 (Incorrect. Should = E)
TextBox1.Text = myList(1)(4) '= 5 (Correct)

Could you please explain what is happening?

Thank you

Nov 29 '05 #2
The reason being is that you are adding values by reference into an
ArrayList. You have only one reference, you are just changing the values (in
that reference). So whatever you last set the values of the array to be,
that is what all references to that array will give you. His code, if you
missed it, creates a whole new array, so now you have two references to two
different arrays in your ArrayList.

Tim

"unwantedspam" wrote:
Hello All,
Thank you in advance. I have a quick question about array lists. Why
does the following code give me the same results if I reference two
different elements?

Dim myList As New ArrayList
Dim test(9) As String
test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"
test(4) = "E"
test(5) = "F"
test(6) = "G"
test(7) = "H"
test(8) = "I"
test(9) = "J"
myList.Add(test)

test(0) = "1"
test(1) = "2"
test(2) = "3"
test(3) = "4"
test(4) = "5"
test(5) = "6"
test(6) = "7"
test(7) = "8"
test(8) = "9"
test(9) = "10"
myList.Add(test)

TextBox1.Text = myList(0)(4) '= 5 (Incorrect. Should = E)
TextBox1.Text = myList(1)(4) '= 5 (Correct)

Could you please explain what is happening?

Thank you

Nov 29 '05 #3
Because arrays are ref types and you are reusing the same referenced object
for both adds. When you set the values the second time, you are actually
setting the values of the object that is already in the list, then you are
adding the list again. You have two instances to the same object in the
list.

"unwantedspam" <un**********@mchsi.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hello All,
Thank you in advance. I have a quick question about array lists. Why
does the following code give me the same results if I reference two
different elements?

Dim myList As New ArrayList
Dim test(9) As String
test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"
test(4) = "E"
test(5) = "F"
test(6) = "G"
test(7) = "H"
test(8) = "I"
test(9) = "J"
myList.Add(test)

test(0) = "1"
test(1) = "2"
test(2) = "3"
test(3) = "4"
test(4) = "5"
test(5) = "6"
test(6) = "7"
test(7) = "8"
test(8) = "9"
test(9) = "10"
myList.Add(test)

TextBox1.Text = myList(0)(4) '= 5 (Incorrect. Should = E)
TextBox1.Text = myList(1)(4) '= 5 (Correct)

Could you please explain what is happening?

Thank you

Nov 29 '05 #4
you added the same string array twice to the arraylist.

so mylist(0) and mylist(1) return the same array and will return the same
results.

try:

Dim myList As New ArrayList
Dim test(9) As String
test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"
test(4) = "E"
test(5) = "F"
test(6) = "G"
test(7) = "H"
test(8) = "I"
test(9) = "J"
myList.Add(test)

ReDim test(9) 'test points to new array
test(0) = "1"
test(1) = "2"
test(2) = "3"
test(3) = "4"
test(4) = "5"
test(5) = "6"
test(6) = "7"
test(7) = "8"
test(8) = "9"
test(9) = "10"
myList.Add(test)

TextBox1.Text = myList(0)(4) '= e
TextBox1.Text = myList(1)(4) '= 5

-- bruce (sqlwork.com)

"unwantedspam" <un**********@mchsi.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hello All,
Thank you in advance. I have a quick question about array lists. Why
does the following code give me the same results if I reference two
different elements?

Dim myList As New ArrayList
Dim test(9) As String
test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"
test(4) = "E"
test(5) = "F"
test(6) = "G"
test(7) = "H"
test(8) = "I"
test(9) = "J"
myList.Add(test)

test(0) = "1"
test(1) = "2"
test(2) = "3"
test(3) = "4"
test(4) = "5"
test(5) = "6"
test(6) = "7"
test(7) = "8"
test(8) = "9"
test(9) = "10"
myList.Add(test)

TextBox1.Text = myList(0)(4) '= 5 (Incorrect. Should = E)
TextBox1.Text = myList(1)(4) '= 5 (Correct)

Could you please explain what is happening?

Thank you

Nov 29 '05 #5

Bruce Barker wrote:
you added the same string array twice to the arraylist.

so mylist(0) and mylist(1) return the same array and will return the same
results.

try:

Dim myList As New ArrayList
Dim test(9) As String
test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"
test(4) = "E"
test(5) = "F"
test(6) = "G"
test(7) = "H"
test(8) = "I"
test(9) = "J"
myList.Add(test)

ReDim test(9) 'test points to new array
test(0) = "1"
test(1) = "2"
test(2) = "3"
test(3) = "4"
test(4) = "5"
test(5) = "6"
test(6) = "7"
test(7) = "8"
test(8) = "9"
test(9) = "10"
myList.Add(test)

TextBox1.Text = myList(0)(4) '= e
TextBox1.Text = myList(1)(4) '= 5

-- bruce (sqlwork.com)

"unwantedspam" <un**********@mchsi.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hello All,
Thank you in advance. I have a quick question about array lists. Why
does the following code give me the same results if I reference two
different elements?

Dim myList As New ArrayList
Dim test(9) As String
test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"
test(4) = "E"
test(5) = "F"
test(6) = "G"
test(7) = "H"
test(8) = "I"
test(9) = "J"
myList.Add(test)

test(0) = "1"
test(1) = "2"
test(2) = "3"
test(3) = "4"
test(4) = "5"
test(5) = "6"
test(6) = "7"
test(7) = "8"
test(8) = "9"
test(9) = "10"
myList.Add(test)

TextBox1.Text = myList(0)(4) '= 5 (Incorrect. Should = E)
TextBox1.Text = myList(1)(4) '= 5 (Correct)

Could you please explain what is happening?

Thank you


Thank you all for you quick and accurate solutions. I greatly
appreciate it.

Nov 29 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
7
by: Adam Hartshorne | last post by:
As a result of a graphics based algorihtms, I have a list of indices to a set of nodes. I want to efficiently identify any node indices that are stored multiple times in the array and the...
21
by: Jeff Thies | last post by:
I'd like to randomly sort an array. A good method?
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
7
by: Jim Lewis | last post by:
I'm trying to move a function into pyrex for speed. The python side needs to pass a list to the pyrex function. Do I need to convert to array or something so pyrex can generate tight code? I'm not...
5
by: bruce | last post by:
hi... i'm trying to deal with multi-dimension lists/arrays i'd like to define a multi-dimension string list, and then manipulate the list as i need... primarily to add lists/information to the...
10
by: J. Peng | last post by:
what's the difference between an array and a list in python? I see list has all features of array in C or perl. so please tell me.thanks.
5
by: Tobiah | last post by:
I checked out the array module today. It claims that arrays are 'efficient'. I figured that this must mean that they are faster than lists, but this doesn't seem to be the case: ...
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
6
by: Jillian Calderon | last post by:
Hi. How do I define a 2d list? For instance, to define a 4 by 5 list, I wanted to do this: n=4 m=5 world = However, it gives me an invalid syntax error saying the index is out of range.
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.