Saving values from arraylist to txt C# [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to save everything from ArrayList to .txt file in c#, then on starting WPF application load it?

static void SaveArray()
{
ArrayList myArray = new ArrayList();
myArray.Add("First");
myArray.Add("Second");
myArray.Add("Third");
myArray.Add("and more");
StreamWriter sw= File.CreateText(#"C:\file.txt");
foreach (string item in myArray)
{
sw.WriteLine(item);
}
sw.Close();
}
and you shouldn't use arraylist not because its 2013 ,but because arraylist boxing each item in the array, where List store the type also.
this cost less in memory use.

Related

Saving ArrayList values to XML C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have ArrayList nabídka, I have to save everything from this ArrayList to XML file using stream writer and on starting application load the XML file to ArrayList with stream reader. Does anybody know how to do it? (Yes, I know that I shouldn not use ArrayList, but i must complete this project with it.)
Heres an example of how it might be implemented
ArrayList sampleList = new ArrayList();
sampleList.Add(" ");
//Add your elements
//StreamWriter initialized with append mode
StreamWriter streamwriter = new StreamWriter(" INSERT PATH OF XML HERE ", true);
for (int i = 0; i < sampleList.Count; i++)
{
//Elements are written into the file here, remember not to forget the xml structure
streamwriter.WriteLine(sampleList[i]);
}
//You have to close the streamwriter or you have to flush to make sure the text is saved
streamwriter.Close();
I hope I could help you
Also possible:
var xmlSerializer = new XmlSerializer(typeof(ArrayList), new Type[] { typeof(YourType) });
But for serializing it would be better to have a List<t>.
The benefit of serializing is the easy read in and that you even can share whole objects easily between languages/applications.

how to use StreamReader in C# to read only entries over a specific length to a list? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
when using StreamReader in C# to load a txt file into a list, i assume that using a simple "If" the string's length is over a particular length, it will add it to the list. can anyone provide C# code for this? this IS homework, but it's NOT a C# class. the instructor would gladly provide this if i asked this specifically. thx.
the txt file is a dictionary of ~280,000 words, one per line. very simple move to turn into a list, but i'm wondering about getting words at least 2 characters long.
Just use LINQ to give you a subset.
List<string> lines = File.ReadLines(filename)
.Where(l => l.Length > specifiedWordLength)
.ToList();

ArrayList<Uri> Intent. How to write in C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to write this in C#?
Intent intent = new Intent(Intent.ActionSendMultiple, Android.Provider.MediaStore.Images.Media.ExternalContentUri);
handler(intent);
....
void handler(Intent intent)
{
ArrayList<Uri> imageUris = intent.GetParcelableArrayListExtra(Intent.ExtraStream);
...
}
ArrayList is available is c# but it does not have a generic (<T>) form. ArrayList is a list of objects.
What you want is System.Collections.Generic.List<T>
EDIT : In response to comments, try this;
List<Uri> imageUris = new List<Uri>(intent.GetIntegerArrayListExtra(Intent.ExtraStream));
Easy, just use List<Uri>. It's also faster than ArrayList.

Remove items from one list in another c# with high performance [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need remove all items step by step from list and add removed items to another list, but I need max effective and hithperformance operation. What is whe best practics?
You could use this code and hope that framework'll do the best for you:
public static void MoveItems<T>(List<T> list1, List<T> list2)
{
list2.AddRange(list1);
list1.Clear();
}
list2 = list1.ToList();
list1.Clear();

What is the C# equivalent for java LinkedList [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the C# equivalent for java LinkedList
you can use Linkedlist in c# and this is good example ...
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Create a new linked list object instance.
LinkedList<string> linked = new LinkedList<string>();
// Use AddLast method to add elements at the end.
// Use AddFirst method to add element at the start.
linked.AddLast("cat");
linked.AddLast("dog");
linked.AddLast("man");
linked.AddFirst("first");
// Loop through the linked list with the foreach-loop.
foreach (var item in linked)
{
Console.WriteLine(item);
}
}
}
C# already has good LinkedList class.
See msdn.
Is it not satisfied your requirement?

Categories