[Solved] Saving values from arraylist to txt C# [closed]

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 … Read more

[Solved] How to arrange the datas to display in array by given string of days in java?

Collections.rotate will do that you want String[] daysArr = { “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat” }; List<String> daysList = Arrays.asList(daysArr); String input = “Fri”; int index = daysList.indexOf(input); if (index > 0) { Collections.rotate(daysList, -index); } System.out.println(daysList); Hope it helps! solved How to arrange the datas to display in array by given string of … Read more

[Solved] Java Remove ArrayList by Value [closed]

Assuming your ArrayList is this: List<String[]> arrayList = new ArrayList<>(); arrayList.add(new String[]{“R111″,”Red”,”50000″}); arrayList.add(new String[]{“R123″,”Blue”,”50000″}); you can do something like: for (Iterator<String[]> iterator = arrayList.iterator();iterator.hasNext();) { String[] stringArray = iterator.next(); if(“R111”.equals(stringArray[0])) { iterator.remove(); } } You can safely remove an element using iterator.remove() while iterating the ArrayList. Also see The collection Interface. An alternative shorter approach … Read more

[Solved] How Java ArrayList works internally [closed]

If you pass an ArrayList to a function it passes a pointer back to the original list. That means any updates in the method will be updating your original ArrayList because they are the same thing. ArrayList<String> list2 = new ArrayList<String>(list); That makes a copy of list and saves it into list2. list and list2 … Read more

[Solved] Random Number generator in a range and then print length of the sequence [duplicate]

Consider using a while-loop: import java.util.Random; class Sequence { public static void main(String[] args) { Random r = new Random(); int count = 0; int num = -1; System.out.println(“The sequence is:”); while (num != 0) { num = r.nextInt(10); System.out.print(num + ” “); count++; } System.out.printf(“%nThe length of the sequence is: %d%n”, count); } } … Read more

[Solved] Java get First and Last duplicate elements of List [closed]

you can try this public static void main(String[] args) { String[] strings = {“one”, “one”, “one”, “one”, “one”, “one”, “one”, “one”, “one”, “one”, “one”, “one” , “two”, “two”, “two”, “two”, “two”, “two”, “two”, “two”, “two”, “two”, “two”, “two”, “two”, “two”, “two” , “three”, “three”, “three”, “three”, “three”, “three”, “three”, “three”, “three”, “three”, “three” , “four”, … Read more

[Solved] ArrayList remove methods [closed]

All the 0’s will not be removed (which is your intention). After the first iteration, your list will be [0, 4, 2, 5, 0, 3, 0]. Now, k=1. You will only be looking from the element 4 at index = 1. Instead, try using Iterators: Example: for (Iterator<String> iter = list.iterator(); iter.hasNext();) { String s … Read more

[Solved] Arraylist for subclasses [duplicate]

I’m guessing it’s the line Program.setInventorie(c1);. You need to create an instance of the Program class and call setInventorie() on that. setInventorie() is not a static method. e.g.: Program myProgram = new Program(); myProgram.setInventorie(c1); 7 solved Arraylist for subclasses [duplicate]

[Solved] How to get the smallest list?

I dont know how you named your Lists, so I’m just going to call them List a, b, c and d. public List<?> getLongestList(List<?> a, List<?> b, List<?> c, List<?> d) { if (a.size() >= b.size() && a.size() >= c.size() && a.size() >= d.size()) return a; if (b.size() >= a.size() && b.size() >= c.size() && … Read more