gods icon indicating copy to clipboard operation
gods copied to clipboard

issue with IndexOf

Open architagr opened this issue 1 year ago • 0 comments

sample code

func getSampleData() []*user {
	return []*user{
		{
			id:   1,
			name: "test 1",
		},
		{
			id:   2,
			name: "test 2",
		},
		{
			id:   3,
			name: "test 3",
		},
		{
			id:   4,
			name: "test 4",
		},
		{
			id:   5,
			name: "test 5",
		},
	}
}
func TestFind(t *testing.T) {
	userList := arraylist.New(getSampleData())
	index := userList.IndexOf(&user{
		id:   1,
		name: "test 1",
	})
	if index == -1 {
		t.Fatalf("Array list should not return -1 as a response of find")
	}
}

from the above test, we can see that the collection has the value that we are trying to get the index for, this is due to the data being of type pointer stored in this and hence the equal operator will not work.

architagr avatar Nov 17 '24 11:11 architagr